I created an application with a set of parameters in it, and you can change the application configuration. Reading from the preset is complete. I am using the code below.
let defs: NSUserDefaults = NSUserDefaults.standardUserDefaults() defs.synchronize() var settingsBundle: NSString = NSBundle.mainBundle().pathForResource("Settings", ofType: "bundle")! if(settingsBundle.containsString("")){ NSLog("Could not find Settings.bundle"); return; } var settings: NSDictionary = NSDictionary(contentsOfFile: settingsBundle.stringByAppendingPathComponent("Root.plist"))! var preferences: NSArray = settings.objectForKey("PreferenceSpecifiers") as! NSArray var defaultsToRegister: NSMutableDictionary = NSMutableDictionary(capacity: preferences.count) for prefSpecification in preferences { if (prefSpecification.objectForKey("Key") != nil) { let key: NSString = prefSpecification.objectForKey("Key")! as! NSString if !key.containsString("") { let currentObject: AnyObject? = defs.objectForKey(key as! String) if currentObject == nil {
The same settings can be updated by the application itself. But how can I write the updated values in the settings? In this link, the accepted answer says it is possible to use NSUserDefaults
. But I could not, can someone help?
EDIT See below for more details:
{ PreferenceSpecifiers = ( { Title = Group; Type = PSGroupSpecifier; }, { DefaultValue = Test1; Key = "multi_values"; Title = "Multiple Values"; Titles = ( FIrst, Second, Third ); Type = PSMultiValueSpecifier; Values = ( FirstValue, SecondValue, ThirdValue ); } ); StringsTable = Root;
}
I have a set of settings, such as the format above, and I can read it in the application. But how can I update this selection of several values in accordance with the changes made in the application? I used the following code to update the value of the Header .
var settings: NSDictionary = NSDictionary(contentsOfFile: settingsBundle.stringByAppendingPathComponent("Root.plist"))! var preferences: NSArray = settings.objectForKey("PreferenceSpecifiers") as! NSArray preferences.objectAtIndex(1).setValue("MutipleValue is updated", forKey: "Title") settings.setValue(preferences, forKey: "PreferenceSpecifiers") settings.writeToFile(settingsBundle.stringByAppendingPathComponent("Root.plist"), atomically: false)
source share