Storing and Saving Data in iOS

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:

  1. 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"]];                               
  2. 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.
  3. You could use sqlite. Code can get a bit tedious and ugly, but it still works fine.
  4. Core Data.

That is all!

One thought on “Storing and Saving Data in iOS

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s