OS X Hello World Swift教程

时间:2019-01-15 02:10:28

标签: swift macos

因此,我正在关注this tutorial for Swift,但最后没有获得理想的结果。这是我的ViewController代码:

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var nameField: NSTextField!

    @IBOutlet weak var helloLabel: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    @IBAction func sayButtonClicked(_ sender: Any) {
        var name = nameField.stringValue
        if name.isEmpty {
            name = "World"
        }
        let greeting = "Hello \(name)!"
        helloLabel.stringValue = greeting
    }

}

结果如下:

enter image description here

但是当我调试程序时,它确实显示了正确的变量:

enter image description here

我在这里做什么错了?

1 个答案:

答案 0 :(得分:2)

结果是我没有足够扩展“标签”字段。