I am trying to write a mutable array of custom objects, which has 5 data objects, to the disk. However, using the unarchiver always makes my array go null, and I cannot figure out why. Here is the method: (self.toDoItems is an NSMutableArray defined in this class)
- (void)viewDidLoad {
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
NSLog(@"toDOitems on load = %@",self.toDoItems);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"deliveries.txt"];
self.toDoItems = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
NSLog(@"toDOitems after unarchive = %@",self.toDoItems);
[self loadInitialData1];
//load array here
}
And here is the output:
2015-06-28 17:15:07.778 ToDoList[13493:645560] toDOitems on load = (
)
2015-06-28 17:15:07.780 ToDoList[13493:645560] toDOitems after unarchive = (null)
2015-06-28 17:15:07.780 ToDoList[13493:645560] toDOitems = (null)
2015-06-28 17:18:42.498 ToDoList[13493:647529]
And here is the implementation of NSCoding:
#import "ToDoItem.h"
@implementation ToDoItem
-(void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:self.total forKey:@"total"];
[encoder encodeObject:self.tip forKey:@"tip"];
[encoder encodeObject:self.percentage forKey:@"percentage"];
[encoder encodeBool:self.completed forKey:@"completed"];
[encoder encodeBool:self.isCreditCard forKey:@"isCreditCard"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.total = [decoder decodeObjectForKey:@"total"];
self.tip = [decoder decodeObjectForKey:@"tip"];
self.percentage = [decoder decodeObjectForKey:@"percentage"];
self.completed = [decoder decodeBoolForKey:@"completed"];
self.isCreditCard = [decoder decodeBoolForKey:@"isCreditCard"];
}
return self;
}
@end