0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
nstein
  • 339
  • 2
  • 14
  • Does your class inherit from NSObject? – Brandon Schlenker Feb 17 '14 at 19:38
  • Yes, I was very careful about that. – nstein Feb 17 '14 at 19:41
  • I solved it! I've been stuck on this issue since yesterday. What never occurred to me was to add multiple NSData objects to plist, i.e. obj3, obj4 etc. Now I just tried that and tried to read the data back using "obj3" and "obj4" and it worked. For some strange reason the "obj2" seamed to be broken or something. – nstein Feb 17 '14 at 19:56
  • NSKeyedArchiver and plists are two different, incompatible ways to serialize objets. What you are creating is not a plist. Don't use the term plist. It is wrong. – Duncan C Feb 17 '14 at 23:44
  • I know, but what I have got here is actually a plist with a Dictionary as root that goes into dic variable. My initial problem was that I wanted to store an object into the plist. This is only possible by converting the object into NSData by serialising it using NSKeyedArchiver. I'm sorry if that wasn't clear before. I wasn't implying that NSKeyedArchivers are plists in any way. – nstein Feb 18 '14 at 20:04

0 Answers0