Problem encoding archiving and saving data
Hello,
I'm trying to archive and save an instance of a (custom) object:
-(void) saveProfile{
ProfileObj *profile = [[ProfileObj alloc] init];
profile = Player[0];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:profile forKey:kDatakey];
[archiver finishEncoding];
[data writeToFile:[self pathOfFile] atomically:YES];
[profile release];
[archiver release];
[data release];
}
for debugging purposes my ProfileObj currently only has one NSString variable that is being saved, and the code runs fine while that NSString is nil (currently loaded from Player[0]) but when the string has data the app crashes at [archiver encodeObject:profile forKey:kDatakey]; and i cannot figure out why or how to fix it. This is my first time ever trying to save data and any help would be greatly appreciated!
#pragma mark NSCoding
-(void) encodeWithCoder:(NSCoder *)encoder {
[encoder endodeObject:Name forKey: kNameKey];
}
-(id) initWithCoder:(NSCoder *)decoder {
if (self = [super init]){
self.Name = [decoder decodeObjectForKey:kNameKey];
}
return self;
}
#pragma mark -
#pragma mark NSCopying
-(id)copyWithZone:(NSZone *)zone{
ProfileObj *copy = [[[self class] allocWithZone: zone] init];
copy.Name = [[self.Name copyWithZone:zone] autorelease];
return copy;
}
This is some code within ProfileObj.h, the one string i'm trying to save is called Name.
|