4

I have an object containing various NSString objects and variables which I us NSCoding to archive to a file on the disk and later unarchive. So far everything has been working perfectly.

Today, I wanted to add an NSMutableArray to the object and tried to encode using the:

[encoder encodeObject:myArray ForKey:@"myArray"];

and later decode it using:

[self setMyArray:[decoder decodeObjectForKey:@"myArray"]];

It doesn't appear to be working, and while I don't get any errors in the encoding or decoding itself, I do get an error if I try to modify the value of the array after decoding from the file.

I'm sure I'm doing something completely wrong here, but not entirely certain what. I'm thinking perhaps it may have something to do with it not properly allocing during the unarchive.

Anything look blatantly obvious as the source of the problem?

Greg Steiner
  • 647
  • 1
  • 9
  • 20
  • possible duplicate of [NSKeyedArchiver and NSKeyedUnarchiver with NSMutableArray](http://stackoverflow.com/questions/10391803/nskeyedarchiver-and-nskeyedunarchiver-with-nsmutablearray) – Caleb Jun 02 '12 at 00:53
  • 1
    I agree. It is essentially the same issue. I didn't realize it when I created it. Quite honestly, I forgot I had a similar issue once before! Of the two, i think this one is much more concise and has a simple direct answer, while the variation from a few weeks ago is more detailed and includes other related issues that could give rise to this issue. Thanks to everyone who helped on both! :) – Greg Steiner Jun 02 '12 at 01:40
  • Ha! I didn't even notice that you asked the duplicated question. ;-) – Caleb Jun 02 '12 at 03:10

1 Answers1

10

It doesn't appear to be working, and while I don't get any errors in the encoding or decoding itself, I do get an error if I try to modify the value of the array after decoding from the file.

Decoding an archive gives you immutable objects regardless of whether they were mutable or immutable when you encoded them. You're not doing anything particularly wrong -- it's working as advertised.

See this answer for another example of a similar problem, and a solution:

[self setMyArray:[[decoder decodeObjectForKey:@"myArray"] mutableCopy];
Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    I would say you mailed it. Apparently I'm having a complete Noob day. I should have seen that.... Especially since the "similar problem" was one of mine from another Noob day a few weeks back. I would give you double upvotes if I could... Just for being patient and pointing me back to an answer I'd already been given. ::: slaps forehead::: – Greg Steiner Jun 02 '12 at 01:18
  • Hm.... made the changes (my NSKeyedUnarchiver was set to RETAIN... I changed that to mutableCopy, and I added mutableCopy to my array decoder method as well) and it still is crashing when I try to addObject: to the array... but only after unarchiving. Any ideas? – Greg Steiner Jun 02 '12 at 03:24
  • Check that the array to which you're trying to add an object is a valid, mutable array. Check that the object you're adding is valid. If those are both okay, there shouldn't be a problem adding the object. – Caleb Jun 02 '12 at 03:56
  • Even with the changes, my Array after unarchiving is showing as an "NSArrayI" rather than "NSArrayM". Here's the decoder line that I'm using for my array: `[self setPerformanceCast:[[decoder decodeObjectForKey:@"performanceCast"] mutableCopy]];` – Greg Steiner Jun 02 '12 at 14:42
  • 1
    I have found the solution: It looks like I didn't save my changes to my property settings for the performanceCast array. Originally it was set to property (copy) instead of property (retain). I had changed it earlier, but must not have saved it. Now everything is working correctly. On a related note, can you recommend a good source I can use to better understand the retain, copy, etc. parameters and memory management in general? I have Stephen Kochan's book, but it's the forth edition with ARC so he's removed a lot of the memory management explanations. – Greg Steiner Jun 02 '12 at 15:08
  • I think your best best is Apple's [*Advanced Memory Management Programming Guide*](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011-SW1). – Caleb Jun 02 '12 at 15:26