简体   繁体   English

为什么NSMutableArray Count使我的应用程序崩溃?

[英]Why does NSMutableArray Count crash my app?

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. 调试器说: [list count]使应用程序崩溃。 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 is 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,但在解码时,您尝试在此处将其作为NSMutableArray取回:

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

And I am guessing your class Adding is not an Array. 而且我猜您的课程Adding不是Array。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM