I added a set of settings to my application, and in Xcode it appeared at the root of my view of the project tree.
The Root.plist
file is as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>StringsTable</key> <string>Root</string> <key>PreferenceSpecifiers</key> <array> <dict> <key>Type</key> <string>PSGroupSpecifier</string> <key>Title</key> <string>Service</string> </dict> <dict> <key>Type</key> <string>PSTextFieldSpecifier</string> <key>Title</key> <string>Hostname</string> <key>Key</key> <string>service_hostname</string>
When I open the Settings app in iOS, the entry appears below, and I can perfectly display and edit my settings.
However, I cannot get these values ββfrom the code. Here is my code:
static func loadSettings() { let ud = NSUserDefaults.standardUserDefaults() ud.synchronize() Settings.hostName = ud.stringForKey("service_hostname")
I also tried ud.objectForKey
and ud.valueForKey
- both returned nil
.
After setting Settings.hostName
the Xcode debugger reports show that it is nil
, despite the fact that I set an explicit value in the Settings application.
I saw this topic ( iPhone App: how to get the default value from root.plist? ), Where someone posted a piece of Objective-C code that manually loads Root.plist
directly into NSMutableDictionary
and calls NSUserDefaults.standardUserDefaults().registerDefaults
, but it looks like a hack (and I can't get it to work in Swift because the compiler says that stringByAppendingPathComponent
no longer exists)
Why doesn't NSUserDefaults
select settings from the Settings app?
source share