我正在编写一个简短的 Mac 应用程序(我们称之为“myApp”),我需要读取和写入 ~/Library/Preferences/中的 .GlobalPreferences plist。
对于阅读部分,我使用以下内容并且工作正常:
let boolValue = NSUserDefaults.standardUserDefaults().boolForKey("aKeyInGlobalPreferences")
但是,我在更改同一键的值时遇到了一些麻烦。我尝试了以下方法:
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "aKeyInGlobalPreferences")
NSUserDefaults.standardUserDefaults().synchronize()
遗憾的是,它没有更改 .GlobalPreferences 中的键值。相反,它创建了一个新的 plist com.myName.myApp,其中包含键值对。
如何让我的应用写入 .GlobalPreferences 而不是创建新的 plist?
最佳答案
您需要使用 setPersistentDomain()
专门写入全局域。
NSUserDefaults.standardUserDefaults().setPersistentDomain(["aKeyInGlobalPreferences":true],
forName: NSGlobalDomain)
关于swift - 使用 Swift 在 .GlobalPreferences 中保存键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34108000/