开发者

NSKeyedUnarchiver does not call initWithCoder

I've used the following example:

game state singleton cocos2d, initWithEncoder always returns null

Instead of using a file, I use an NSData object i am getting from NSUserDefaults. It is not null, but rather have data in it that I believe is the correctly encoded data and for some reason when I run unarchiveObjectWithData it does not call initWithCoder as I put an NSLog on the first line.

Ideas?

+ (void)loadState {
 @synchronized([AppStateManager class]) {
  if (!sharedAppStateManager) {
   [AppStateManager sharedManager];
  }

  NSUserDefaults *udefs = [NSUserDefaults standardUserDefaults];
  NSData *archivedData = [udefs objectForKey:@"AppState"];
  NSLog(@"going to unarchive");
  [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
  NSLog(@"unarchive complete");
 }
}

- (id)initWithCoder:(NSCoder *)decoder {
 NSLog(@"init coder");
 self = [super init];
 if (self !=开发者_JS百科 nil) {
  [self setUsername:[decoder decodeObjectWithKey:@"username"]];
 }

 return self;
}


Solved.

The example posted at the link above initializes the object via "sharedInstance" using the allocWithZone method. If the object exists it returns nil.

the NSKeyedUnarchiver tries to alloc AGAIN via allocWithZone and since the object has already been initialized earlier in that very same method, or sometimes before hand the unarchiver gets a nil instead of an allocated object.

Hope this helps someone.


I don't know why, but after I do this to obtain my NSData of the archive (the contents of which i think is inconsequential):

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *encodedConfig = [userDefaults valueForKey:kConfiguration];

When I execute this, it returns nil.

NSArray *configs = [NSKeyedUnarchiver unarchiveObjectWithData:encodedConfig];

But when I execute this, I get the expected, previously archived, array of objects.

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:encodedConfig];
NSArray *configs = [unarchiver decodeObject];

I expect identical behavior from these two approaches. My only response is avoid unarchiveObjectWithData:.


This isn't so much an answer but a bit of additional info I found while trying to work a similar issue out. I guess I am still under rookie status, so this is the only way I could find to contribute to this post.

Anyways... Thanks so much for banging through this issue...especially over here: http://www.cocos2d-iphone.org/forum/topic/11327

I was having a problem with a mutable array working using the method you had worked out where it would switch to an immutable array after I unarchived it from the file.

Casting the returned array as a mutable version did not work either. I end up adding objectsFromArray to my mutable array and this solved my problems.

Worked:

[self.mutableArray addObjectsFromArray:[decoder decodeObjectForKey:@"MutableArray"]];

Neither of these worked:

self.mutableArray = [decoder decodeObjectForKey:@"MutableArray"];

self.mutableArray = (NSMutableArray *)[decoder decodeObjectForKey:@"MutableArray"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜