NSMutableArray Encoding

I have an object containing various NSString objects and variables, which I find for NSCoding to archive to a file on disk, and then using unarchive. So far, everything is working fine.

Today I wanted to add NSMutableArray to an object and tried to code using:

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

and then decode it using:

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

It does not work, and until I get any errors in the encoding or decoding itself, I get an error message if I try to change the value of the array after decoding from the file.

I'm sure I'm doing something completely wrong here, but not quite sure what. I think it is possible that this may have something to do with this, not highlighting correctly during unpacking.

Does anything look clearly obvious as the source of the problem?

+4
source share
1 answer

It does not work, and until I get any errors in the encoding or decoding itself, I get an error message if I try to change the value of the array after decoding from the file.

Decoding an archive gives you immutable objects, regardless of whether they are mutable or immutable when encoded. You are not doing anything particularly bad - it works as advertised.

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

 [self setMyArray:[[decoder decodeObjectForKey:@"myArray"] mutableCopy]; 
+10
source

Source: https://habr.com/ru/post/1415671/


All Articles