トップ «前の日記(2017-01-22) 最新 次の日記(2017-01-28)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2017-01-23 [macOS]始めよう

macOSプログラミングがマイブームだ。手始めに、初期のヒレガス本を最新の開発環境で頭からやってみることにする。

新規プロジェクトを作成。macOSでCocoa Applicationを選択。

新規プロジェクト

以前と異なるのは、以前は自分でアプリケーションのコントローラとなるクラスAppControllerを生成したが、最新の開発環境では、iOSと同様に、AppDelegateとViewControllerが生成済み。

以前ならAppDelegateを選択したと思うが、ViewControllerにコントローラの処理を追加。

AppDelegateは、こんな感じ。

import Cocoa
 
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
    }
 
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

ViewControllerに乱数生成のコードを追加。

import Cocoa
import GameplayKit
 
class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
 
    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    @IBOutlet var textField: NSTextField?
 
    @IBAction func generate(sender: NSButton) {
        let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: 99) + 1
        textField?.integerValue = randomNumber
    }
    
    @IBAction func seed(sender: NSButton) {
        textField?.stringValue = "Generator seeded"
    }
}

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/mac/RandomApp - GitHub

_ 【Cocoa練習帳】

http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)

トップ «前の日記(2017-01-22) 最新 次の日記(2017-01-28)» 編集