1
votes

I want to make an iOS app where the users can store lists of basic objects. These objects will only have something like 3-4 properties each.

The user could create as many of these lists (NSArray) as possible.

I have two questions:

  1. How would I go about saving these NSArrays to disk? Some people say to use NSUserDefaults and others say to use NSKeyedArchiver.

  2. What is the best way to keep track of these arrays? Would it be wise to use an array to store these arrays?

Thanks in advance!

3

3 Answers

1
votes

If your arrays contain all property list objects (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects) then you can save your array (or array of arrays) directly to a plist or to user defaults.

If you have any non property-list objects in your array then you will need to conform to the NSCoding protocol and use NSKeyedArchiver.

Property lists are easier.

0
votes

you can use arrays to store your arrays.
and use plist for saving these to an array so you can have an advantage of saving in the same format as you want in memory for displaying the result.
you can read following tutorial for plist enter link description here

0
votes
        //Initialize an array with 3 objects and store to NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfUserA = [[NSArray alloc] initWithObjects:@"Obj1", @"Obj2", @"Obj3", nil];
[defaults setValue:arrayOfUserA forKey:@"arrayOfUserA"];
[defaults synchronize];

//Get the array back, modify and save again
arrayOfUserA = [defaults valueForKey:@"arrayOfUserA"];
NSMutableArray *modifiedArray = [[NSMutableArray alloc] initWithArray:arrayOfUserA copyItems:YES];
[modifiedArray addObject:@"Obj4"];
arrayOfUserA = [modifiedArray copy];
[defaults setValue:arrayOfUserA forKey:@"arrayOfUserA"];
[defaults synchronize];