何时注册用户默认值?

时间:2016-12-28 20:18:52

标签: swift macos swift3

我正在编写我的第一个macOS应用程序,但我无法找到注册用户默认值的位置。我需要使用viewDidLoad中的值。在我的iOS应用程序中,我通常在applicationDidFinishLaunching中注册它,但macOS中的启动顺序是不同的?

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let dict = ["defaultPath": "/Users/mike/file.log"]
        UserDefaults.standard.register(defaults: dict)
    }
}

class ViewController: NSViewController {
    @IBOutlet weak var textField: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Error here since user default has not been registered
        textField.stringValue = UserDefaults.standard.string(forKey: "defaultPath")!
    }
}

0 个答案:

没有答案