Posted By

espinallab on 01/29/10


Tagged

settings plist Objective-c iphone


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

umang_nine
avanadra
bobbym245
cnathan


Updating & changing settings plist files with new versions of an app


 / Published in: Objective C
 

URL: http://stackoverflow.com/questions/1609305/updating-changing-settings-plist-files-with-new-versions-of-an-app

This code works

  1. - (void)readSettings {
  2.  
  3. // Get Paths
  4. NSString *defaultSettingsPath = [[NSBundle mainBundle] pathForResource:@"DefaultSettings" ofType:@"plist"];
  5. NSString *settingsPath = [self.applicationDocumentsPath stringByAppendingPathComponent:@"Settings.plist"];
  6.  
  7. // Read in Default settings
  8. self.settings = [NSMutableDictionary dictionaryWithContentsOfFile:defaultSettingsPath];
  9.  
  10. // Read in Current settings and merge
  11. NSDictionary *currentSettings = [NSDictionary dictionaryWithContentsOfFile:settingsPath];
  12. for (NSString *key in [currentSettings allKeys]) {
  13. if ([[self.settings allKeys] indexOfObject:key] != NSNotFound) {
  14. if (![[currentSettings objectForKey:key] isEqual:[self.settings objectForKey:key]]) {
  15.  
  16. // Different so merge
  17. [self.settings setObject:[currentSettings objectForKey:key] forKey:key];
  18.  
  19. }
  20. }
  21. }
  22.  
  23. }
  24.  
  25. - (void)saveSettings {
  26. if (self.settings) {
  27. NSString *settingsPath = [self.applicationDocumentsPath stringByAppendingPathComponent:@"Settings.plist"];
  28. [self.settings writeToFile:settingsPath atomically:YES];
  29. }
  30. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: the_coder on January 19, 2011

for (NSString *key in [currentSettings allKeys]) should be written as for (NSString *key in currentSettings)

if ([[self.settings allKeys] indexOfObject:key] != NSNotFound) should be written as if ([self.settings objectForKey:key])

You need to login to post a comment.