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?

3
  • possible duplicate of NSKeyedArchiver and NSKeyedUnarchiver with NSMutableArray Commented Jun 2, 2012 at 0: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! :) Commented Jun 2, 2012 at 1:40
  • Ha! I didn't even notice that you asked the duplicated question. ;-) Commented Jun 2, 2012 at 3:10

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

6 Comments

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:::
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?
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.
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]];
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.