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. 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.
精彩评论