I'll link a post from the inimitable NSHipster on the subject. http://nshipster.com/nscoding/
NSCoding is a simple protocol, with two methods: -initWithCoder: and encodeWithCoder:. Classes that conform to NSCoding can be serialized and deserialized into data that can be either be archived to disk or distributed across a network.
@interface Book : NSObject <NSCoding>
@property NSString *title;
@property NSString *author;
@property NSUInteger pageCount;
@property NSSet *categories;
@property (getter = isAvailable) BOOL available;
@end
@implementation Book
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
}
self.title = [decoder decodeObjectForKey:@"title"];
self.author = [decoder decodeObjectForKey:@"author"];
self.pageCount = [decoder decodeIntegerForKey:@"pageCount"];
self.categories = [decoder decodeObjectForKey:@"categories"];
self.available = [decoder decodeBoolForKey:@"available"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.title forKey:@"title"];
[encoder encodeObject:self.author forKey:@"author"];
[encoder encodeInteger:self.pageCount forKey:@"pageCount"];
[encoder encodeObject:self.categories forKey:@"categories"];
[encoder encodeBool:[self isAvailable] forKey:@"available"];
}
@end
Once your class is setup you can save/restore from disk easily:
// Archive
[NSKeyedArchiver archiveRootObject:books toFile:@"/path/to/archive"];
// Unarchive
[NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/archive"];
So in your case you have a class like this:
@property (nonatomic, strong) NSString *className;
@property (nonatomic, strong) NSString *assignmentDescription;
@property (nonatomic, strong) NSString *assignmentTitle;
@property (nonatomic, strong) NSString *dateTimeString;
@property (nonatomic, assign) BOOL notifcationStatus;
So you'd end up with an initWithCoder method like:
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
}
self.className = [decoder decodeObjectForKey:@"className"];
self.assignmentDescription = [decoder decodeObjectForKey:@"assignmentDescription"];
self.assignmentTitle = [decoder decodeIntegerForKey:@"assignmentTitle"];
self.dateTimeString = [decoder decodeObjectForKey:@"dateTimeString"];
self.notifcationStatus = [decoder decodeBoolForKey:@"notifcationStatus"];
return self;
}
and an encodeWithCoder like:
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.className forKey:@"className"];
[encoder encodeObject:self.assignmentDescription forKey:@"assignmentDescription"];
[encoder encodeInteger:self.assignmentTitle forKey:@"assignmentTitle"];
[encoder encodeObject:self.dateTimeString forKey:@"dateTimeString"];
[encoder encodeBool:self.notifcationStatus forKey:@"notifcationStatus"];
}
Now you should be able to just add your newly created objects to an NSMutableArray, and when necessary save that array to disk/load it from disk.