0

我遇到了一个问题。每次我启动我的应用程序时,它都会崩溃。这是我的代码。调试器说:[list count]应用程序崩溃。我不知道。NSLog(@"%@", self.list);正如预期的那样给了我一件东西......

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]);

打开的存档是这样创建的:

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];

如果您需要任何进一步的代码,请告诉我。我会非常感谢任何帮助:)

4

1 回答 1

1

我相信问题在于您在这里对添加类进行编码:

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

这还不是 NSMutableArray,当您解码时,您正试图在此处将其作为 NSMutableArray 取回:

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

而且我猜你的类添加不是一个数组。

于 2010-02-08T17:12:45.980 回答