Scenario:
Settings.bundle within the host application.
Goal:
To access the data within the settings.bundle.
Modus Operandi:
To dump the settings data into a dictionary for in-program access.
Here's my first attempt. I tried accessing the path, then the bundle. Both didn't work:
let path = NSBundle.mainBundle().pathForResource("Settings", ofType: "bundle")
let myBundle = NSBundle(path: path!)
let myData = NSDictionary(contentsOfFile:path!)
What's the correct syntax of collecting data from the Settings.bundle (which contains the root.plist)?
You can access the saved values via NSUserDefaults
.
For example if you want to get the previously set value ( "bar"
) from the setting named as "foo", then the code to get it is:
let valueOfFoo = NSUserDefaults.standardUserDefaults().valueForKey("foo")
In this case the content of valueOfFoo
variable will be "bar"
.
Note: this returns an Optional
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.