我正在编写我的第一个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")!
}
}