Advertisement
julong

Data Persistence

Mar 30th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  2.  
  3. [userDefaults setBool:YES forKey:@"Key1"];
  4. [userDefaults setInteger:123 forKey:@"Key2"];
  5. [userDefaults setObject:@"Some Object" forKey:@"Key3"];
  6.  
  7. [userDefaults boolForKey:@"Key1"];
  8. [userDefaults integerForKey:@"Key2"];
  9. [userDefaults objectForKey:@"Key3"];
  10.  
  11. [userDefaults synchronize];
  12.  
  13.  
  14. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  15. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16.  
  17. NSArray *fruits = @[@"Apple", @"Mango", @"Pineapple", @"Plum", @"Apricot"];
  18.  
  19. NSString *filePathFruits = [documents stringByAppendingPathComponent:@"fruits.plist"];
  20. [fruits writeToFile:filePathFruits atomically:YES];
  21.  
  22. NSDictionary *miscDictionary = @{@"anArray" : fruits, @"aNumber" : @12345, @"aBoolean" : @YES};
  23.  
  24. NSString *filePathDictionary = [documents stringByAppendingPathComponent:@"misc-dictionary.plist"];
  25. [miscDictionary writeToFile:filePathDictionary atomically:YES];
  26.  
  27. NSArray *loadedFruits = [NSArray arrayWithContentsOfFile:filePathFruits];
  28. NSLog(@"Fruits Array > %@", loadedFruits);
  29.  
  30. NSDictionary *loadedMiscDictionary = [NSDictionary dictionaryWithContentsOfFile:filePathDictionary];
  31. NSLog(@"Misc Dictionary > %@", loadedMiscDictionary);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement