Frederick C. Lee
Frederick C. Lee

Reputation: 9503

How do I programmatically access the Settings.bundle?

Scenario:
Settings.bundle within the host application. enter image description here

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)?

Upvotes: 1

Views: 607

Answers (1)

Roland Kákonyi
Roland Kákonyi

Reputation: 593

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

Upvotes: 1

Related Questions