开发者

Why does NSMutableArray Count crash my app?

开发者 https://www.devze.com 2022-12-19 18:37 出处:网络
I\'ve ran into a problem. Everytime I\'am starting my app it crashes. Heres me code. The Debugger Says: [list count] crashes the app. I have no idea. NSLog(@\"%@\", self.list); gives me one item as ex

I've ran into a problem. Everytime I'am starting my app it crashes. Heres me code. The Debugger Says: [list count] crashes the app. I have no idea. NSLog(@"%@", self.list); gives me one item as expected...

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  data = [[NSData alloc] initWithContentsOfFile:filePath];
  unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

  NSMutableArray *array = [unarchiver decodeObjectForKey:@"TOWN"];
  [unarchiver finishDecoding];
  [unarchiver release];
  [data release];

}

  self.list = array;
  NSLog(@"%@", self.list);
  NSLog(@"count %i", [list count]);

The archive which i开发者_如何学编程s opened was created like that:

Adding *adding = [[Adding alloc] init];
adding.nummer = 1;
adding.stadt = stadt.text;


NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                             initForWritingWithMutableData:data];
[archiver encodeObject:adding forKey:@"TOWN"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];

If you need any futher code let me know. I would be very thankfull for any help :)


I believe the problem is that you are encoding the Adding Class here:

[archiver encodeObject:adding forKey:@"TOWN"];

which is not an NSMutableArray yet when you are decoding you are trying to get it back as an NSMutableArray here:

NSMutableArray *array = [unarchiver decodeObjectForKey:@"TOWN"];

And I am guessing your class Adding is not an Array.

0

精彩评论

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

关注公众号