How to load object after saving with encodeWithCoder?
Posted
by fuzzygoat
on Stack Overflow
See other posts from Stack Overflow
or by fuzzygoat
Published on 2010-03-16T18:47:26Z
Indexed on
2010/03/17
12:21 UTC
Read the original article
Hit count: 330
objective-c
|cocoa
EDIT_002: Further rewrite: if I save using the method below how would the method to load it back in look? (moons is an NSMutableArray of NSNumbers)
// ------------------------------------------------------------------- **
// METHOD_002
// ------------------------------------------------------------------- **
-(void)saveMoons:(NSString *)savePath {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[moons encodeWithCoder:archiver];
[archiver finishEncoding];
[data writeToFile:savePath atomically:YES];
[archiver release];
[data release];
}
EDIT_003: Found it, my problem was that I was using ...
[moons encodeWithCoder:archiver];
where I should have been using ...
[archiver encodeObject:moons];
Hence the loader would look like:
-(void)loadMoons_V3:(NSString *)loadPath {
NSData *data = [[NSData alloc] initWithContentsOfFile:loadPath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[self setMoons:[unarchiver decodeObject]];
[unarchiver finishDecoding];
[unarchiver release];
[data release];
}
gary
© Stack Overflow or respective owner