Im having issues with NSKeyedArchiver. I'm trying to archive a dictionary, which in turn contains arrays of custom objects, Album and Song.
Album and Song both inherent from NSObject and and conform to the NSCoding protocol. Here is how ive implemented the protocol:
Album implementation file:
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
self.title = [aDecoder decodeObjectForKey:@"title"];
self.artistName = [aDecoder decodeObjectForKey:@"artistName"];
self.songs = [aDecoder decodeObjectForKey:@"songs"];
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.artistName forKey:@"artistName"];
[aCoder encodeObject:self.songs forKey:@"songs"];
}
Song Implementation file:
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
self.title = [aDecoder decodeObjectForKey:@"title"];
self.artistName = [aDecoder decodeObjectForKey:@"artistName"];
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.artistName forKey:@"artistName"];
}
title and artistName properties are both NSStrings, white the songs property is an array of Song objects.
I place arrays of these objects into a dictionary called ipodDictForArchiving, then archive the dictionary like so:
-(NSData *)dataForClient {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:[self ipodDictForArchiving] forKey:kDataArchive];
[archiver finishEncoding];
NSLog(@"encoded dictionary");
return data;
}
I then unarchive the dictionary like this:
-(NSDictionary *)unarchiveData:(NSData *)data {
NSData *clientData = data;
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:clientData];
NSDictionary *myDictionary = [unarchiver decodeObjectForKey:kDataArchive];
[unarchiver finishDecoding];
return myDictionary;
}
For some reason, dictionary contains the Song and Album objects, but those objects return null for their properties. I can't figure this out any help greatly appreciated!!
Thanks