Write to Swift preset

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 { // not readable: set value from Settings.bundle let objectToSet: AnyObject? = prefSpecification.objectForKey("DefaultValue") var objectKey : String = key as! String var finalObject : String = objectToSet as! String defaultsToRegister.setObject(finalObject, forKey: objectKey) }else{ //already readable: don't touch var objectKey : String = key as! String var finalObject : String = currentObject as! String defaultsToRegister.setObject(finalObject, forKey: objectKey) } } } } defs.registerDefaults(defaultsToRegister as [NSObject : AnyObject]) defs.synchronize() 

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) 
+2
source share
1 answer

The link you provided answers your question

Please note that you should not read directly from the parameter set, as this does not make sense . You should always retrieve and set custom defaults using NSUserDefaults . When a user makes changes to settings, NSUserDefaults will automatically reflect this. They will always be in sync.

When you create a new item in settings.bundle , you must set its identifier .

After that, you can get the desired value using NSUserDefaults as follows:

 if let export_enabled = NSUserDefaults.standardUserDefaults().valueForKey("export_enabled") as? Bool { if let export_interval = NSUserDefaults.standardUserDefaults().valueForKey("export_interval") as? Number { // ... } } 

And change:

 NSUserDefaults.standardUserDefaults().setValue(true, forKey: "export_enabled") 

Update

The iOS application on the device has the following structure:

 AppTitle.app AppTitle AppTitleIcon.png Info.plist com.company.apptitle.plist (read/write) settings.bundle (readonly) 

settings.bundle , designed to form the default configuration hierarchy Settings.app . It contains section headings and settings types, but settings values ​​are stored in com.company.apptitle.plist .

Since settings.bundle is read-only, you cannot change its structure or section names. You can only change parameter values, and this change occurs in com.company.apptitle.plist via NSUserDefaults .

+4
source

Source: https://habr.com/ru/post/1014481/


All Articles