0

I have an NSMutableArray in which I store objects called "address". An address is an NSObject with 4-5 properties (NSStrings). The NSMutableArray shall contain a maximum of 15 address object.

What is the best way to store that array on the iPhone? Core data? NSUserDefaults? Should I maybe store every address object by itself, and not all objects in one NSMutableArray? In that case what should I do on the iPhone?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zhuber
  • 5,364
  • 3
  • 30
  • 63
  • Serialize objects with NSCoding protocol [guide](http://www.raywenderlich.com/1914/how-to-save-your-app-data-with-nscoding-and-nsfilemanager) – TheBlack Apr 19 '13 at 23:25

3 Answers3

4

as @rog said, you may use NSUserDefaults to save data

& you should make your object follow protocal NSCoding

for examplem if you object is "YouObject"

@interface YouObject: NSObject {

}

@property (nonatomic, copy) NSString *uid;
@property (nonatomic, copy) NSString *name;

@end


//implement this 2 method 

- (id)initWithCoder:(NSCoder *)decoder {
  if (self = [super init]) {
    self.title = [decoder decodeObjectForKey:@"uid"];
    self.author = [decoder decodeObjectForKey:@"name"];
  }
  return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
  [encoder encodeObject:title forKey:@"uid"];
  [encoder encodeObject:author forKey:@"name"];
}

then archive or unarchive using NSUserDefaults

//archive
YouObject *object = [YouObject ....]
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object ];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"address"];

//unarchive
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"address"];
YouObject *object = (YouObject *)[NSKeyedUnarchiver unarchiveObjectWithData:data];

or if you have a YouObject Array, you can save the NSArray in the same way;

//archive
NSArray *addresses;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:address ];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"address"];

//unarchive
NSData *addressData = [[NSUserDefaults standardUserDefaults] objectForKey:@"address"];
NSArray *addresses = (NSArray*)[NSKeyedUnarchiver unarchiveObjectWithData:address];
adali
  • 5,977
  • 2
  • 33
  • 40
  • I'm creating/reading my address object in MainViewController where my tableView is (populate tableView with address properties). Do I have to put all those methods you mentioned in MainViewController then ? Because Address.h and .m only contain declaration and synthesize of 4 properties and nothing else. – zhuber Apr 20 '13 at 13:13
  • you may check this & you will know why the code works http://soff.es/archiving-objective-c-objects-with-nscoding – adali Apr 20 '13 at 13:19
  • To save and load the item at the same time can I use this: [self saveAddressObject:currentAddress]; Address *adr = [self loadAdressObjectWithKey:@"address"]; – zhuber Apr 20 '13 at 14:46
  • And when I'm initializing Address object do i use alloc] init] or alloc] initwithcoder ?? what is coder argument if so ? – zhuber Apr 20 '13 at 14:47
  • use alloc] init] , the initwithcoder is only used for archive – adali Apr 20 '13 at 14:47
  • One more question. If I'm storing and loading using your methods above, how can I save for example 10 address objects if forKey: is the same @"address" ? it overrides and it always loads last one.. one options may be store all in array and then load, but is there any other way to save every object byitself ? – zhuber Apr 20 '13 at 15:00
  • save in a array is the best way , you don't have to handle every address, or you have to remember every "key" of the address, and it's hard ,even impossible, and unnecessary – adali Apr 20 '13 at 15:03
  • if i'm dealing with nsmutablearray with my addresses do I need mutable for defaults or nsarray is fine because i override that data everytime i save ? – zhuber Apr 20 '13 at 15:12
  • So NSArray would be ok? Tnx alot, you really helped. – zhuber Apr 20 '13 at 15:20
1

For what you're describing, I think NSUserDefaults will suffice. See this post: How to store custom objects in NSUserDefaults. You can learn more about the limitations of NSUserDefaults here: What are the limitations of NSUserDefaults.

However, if you're saving/loading a large amount of data, then you should consider using Core Data.

Community
  • 1
  • 1
rog
  • 5,351
  • 5
  • 33
  • 40
0

Simplest way would to use the nsmutablearray read and write methods. All the data has to be plist data type nsarray nsdictionary nsstring nsnumber nsdate. Nsuserdefault like rog suggested is also good. As long as the amount of data remains small.

ejkujan
  • 271
  • 1
  • 3