I've created a class that conforms to NSCoding
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.type = [aDecoder decodeObjectForKey:@"type"];
}
return self;
}
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.type forKey:@"type"];
}
Then the methods used for saving to and reading from the plist are as follows:
- (IBAction)read:(id)sender
{
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:self.plistDocPath];
//NSLog(@"plist contents: %@",dic);
NSData *dataObj = [dic objectForKey:@"obj2"];
NSLog(@"dataObj: %@",dataObj);
ObjClass *obj = [NSKeyedUnarchiver unarchiveObjectWithData:dataObj];
NSLog(@"decoded object: %@",obj);
}
- (IBAction)write:(id)sender
{
ObjClass *obj = [[ObjClass alloc] initWithName:@"nick" andType:@"human"];
//NSString *str = @"example";
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:self.plistDocPath];
NSString *key = [NSString stringWithFormat:@"obj%i",dic.count];
NSData *objData = [NSKeyedArchiver archivedDataWithRootObject:obj];
[dic setObject:objData forKey:key];
[dic writeToFile:self.plistDocPath atomically:YES];
}
The writing to process seems to be working fine if I look into the file I'm seeing an NSData object for key "obj2" with bytes in it.
The problem is when I try to read from the file I'm getting
2014-02-17 20:48:53.780 FileManagerSample[589:70b] *** NSForwarding: warning: object 0x1661f24 of class 'Object' does not implement methodSignatureForSelector: -- trouble ahead
2014-02-17 20:48:53.780 FileManagerSample[589:70b] *** NSForwarding: warning: object 0x1661f24 of class 'Object' does not implement doesNotRecognizeSelector: -- abort
Can someone please help because I'm stuck big time.