views:

253

answers:

2

The App will save the NSMutableArray into a archive with no problems but as soon as I try and load the NSMutableArray back into a new NSMutableArray @ viewDidLoad the app will crash. I put a break point at the end of the code where "tempArray = [unarchiver decodeObjectForKey:kDataKey46];" and tempArray is being loaded with the archived array but when I go through the "for" loop @ "[poolListData addObject:testTemp];" "poolListData" does not hold the data from "tempArray". Also if I did not use a break point and just let the app try to load, the app will crash...What do you guys think?

Thank you for your time! Jeff

 NSString *filePath = [self dataFilePath];
 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
 {
  NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
  NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

  field1.text = [unarchiver decodeObjectForKey:kDataKey1];
  // snip another bunch of fields
  field37.text = [unarchiver decodeObjectForKey:kDataKey37];

  soundOn = [unarchiver decodeBoolForKey:kDataKey38];
  soundVolumeSlider.value = [unarchiver decodeDoubleForKey:kDataKey39];
  soundVolumeValue = [unarchiver decodeDoubleForKey:kDataKey39];
  tempArray = [unarchiver decodeObjectForKey:kDataKey46];

  [unarchiver finishDecoding];

  for(int i = 0; i < [tempArray count]; i++)
  {
   NSData *testTemp = 0;
   //NSString *temp = [tempArray objectAtIndex:i];
   testTemp = [tempArray objectAtIndex:i];
   [poolListData addObject:testTemp];
  //[poolListData addObjectsFromArray:tempArray];
  }

  /*
   firstNameTextField.text = field1.text;
   lastNameTextField.text = field2.text;
   adressTextField.text = field3.text;
   emailTextField.text = field4.text;
   */

  [tempArray release];
  [unarchiver release];
  [data release];
 }
A: 

If you call unarchive, and are not seeing any values, then the array held something that could not be archived. I believe UIImage would be an example of something you might have in the array to try and save. The key to your problem is, what types do you have in that tempArray when you archive it.

Kendall Helmstetter Gelner
If the problem was a UIImage, then his code would break when he archives, and not at the unarchive stage like he says.
Kevlar
It doesn't break. It simply does not write. I have seen this in practice myself, when trying to save UIImages into NSUser defaults. They don't get written out, and you are given no notice.
Kendall Helmstetter Gelner
+2  A: 

I believe this is at least partially a memory management error. The decodeObjectForKey call returns an autoreleased object, so that it’s a bug to release the tempArray at the end of the function. This would be the reason your app crashes. As for poolListData not holding the unarchived array’s contents, maybe you simply forgot to initialize it and you are trying to add items into a nil array?

zoul
Thank you!!! That is what I needed to do
Jeff
P.S. Killer icon!!!
Jeff