views:

381

answers:

2

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

A: 

Can you show the code that defines the list property? Is it a retained property?

Greg Martin
Ah sorry. yes it's retained.<code>NSMutableArray *list;...@property (nonatomic, retain) NSMutableArray *list;</code>
rdesign
I don't understand why my log says: *** -[Adding count]: unrecognized selector sent to instance 0x3b266f0The method I call has nothing to do with "Adding" it should count the self.list but why says it Adding count?!
rdesign
+1  A: 

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.

OscarMk
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
rdesign
I just looked into the sample, you can do that in there because @"Presidents" is a pList file.
OscarMk
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...
rdesign
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.
OscarMk