How do I add settings to my cocoa application in swift?
The NSUserDefaults
class is very easy to use in code, and its shared instance is readily available for binding to controls in Interface Builder.
For example, if I wanted to have an integer preference named "elmer" and set its value to 7, it's as easy as:
NSUserDefaults.standardUserDefaults().setInteger(7, forKey: "elmer")
To read the value back:
let elmer: Int = NSUserDefaults.standardUserDefaults().integerForKey("elmer")
To bind the value to a control in Interface Builder, set the Controller Key to "values", and the preference name for the Model Key Path:
I would recommend reading the "Preferences and Settings Programming Guide", and also to familiar yourself with the "NSUserDefaults Class Reference".
SWITF 5.x
The class changed the name so now you do:
UserDefaults.standard.set("1234", forKey: "userID")
To set a key that can hold any type. Or you can be type specific like this
UserDefaults.standard.bool(forKey: "IsConfigured")
The UI binding still working in the same fashion @ElmerCat well explained.