So, this will be a fairly short post, but I frequently get asked about storing data in iOS.
Essentially, when it comes to storing data in iOS you have several options:
- NSUserDefaults. If the amount of information you want to save isn’t terribly large, and consists of NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary, you can easily store it in NSUserDefaults. NSUserDefaults can be read/written to in much the same way you would work with a NSDictionary. For example:
NSString *test = [NSString alloc]init]; [test stringByAppendingString:@"test!"]; //creates string object NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults]; [myDefaults setObject:test forKey:@"string"]; [myDefaults synchronize]; //and this data can be accessed by: NSString *newTest = [NSString alloc]initWithString:[myDefaults objectForKey:@"string"]];
- If you REALLY insist on writing to a local text file of some sort, do not put it in your application bundle. The bundle is read only, and you will spend an absurd amount of time debugging this.
- You could use sqlite. Code can get a bit tedious and ugly, but it still works fine.
- Core Data. http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started
That is all!
Thanks man, very nice tip. But how can you save on a .plist file?