0

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 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 :)

danielreiser
  • 5,292
  • 5
  • 31
  • 43

1 Answers1

1

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.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • Your right, if i decode it as a class of Adding it works. But as stated in a book the assign it to an nsmutablearray. http://apress.com/book/downloadfile/4476 (If you want to you can have a look at example No. 09) In the PresidentsViewController.m – danielreiser Feb 08 '10 at 17:29
  • I just looked into the sample, you can do that in there because @"Presidents" is a pList file. – Oscar Gomez Feb 08 '10 at 17:43
  • Thanks for your Help! yes, but its archived. the one I've created is made the same way. I've just notived something. The archive they made has as last entry definition as NSMutableArray. Mine gets a def of Adding. How is it possible to change that? EDIT: Item 138 shows the classes... – danielreiser Feb 08 '10 at 17:46
  • Glad I can help, Do you mean how to change the p list defition?. If you look into the Presidents.pList file you will notice $objects is an Array, you can just click on "Array" to change the type. – Oscar Gomez Feb 08 '10 at 18:03