NSUserDefaults is for user settings, not for storing application data. Use CoreData or serialize objects into a document directory. To do this, you need your class to implement the NSCoding protocol.
1) Add NSCoding to Occasion.h
@interface Occasion : NSObject <NSCoding>
2) Implement the protocol in Occasion.m
- (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.title = [aDecoder decodeObjectForKey:@"title"]; self.date = [aDecoder decodeObjectForKey:@"date"]; self.imagePath = [aDecoder decodeObjectForKey:@"imagePath"]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:title forKey:@"title"]; [aCoder encodeObject:date forKey:@"date"]; [aCoder encodeObject:imagePath forKey:@"imagePath"]; }
3) Archive the data to a file in the document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *path= [documentsPath stringByAppendingPathComponent:@"occasions"]; [NSKeyedArchiver archiveRootObject:occasions toFile:path];
4) To unlock ...
NSMutableArray *occasions = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
source share