0

In myClass.h, I have

@property (copy, atomic) NSMutableDictionary *thisParticularWordList;

In myClass.m I populate thisParticularWordList as follows:

theSubview.thisParticularWordList = [NSMutableDictionary dictionaryWithDictionary: someDictionary];

In the variables view of xcode I see, that the property of this instance is indeed populated.

Elsewhere in my code, I try to do this:

[self.thisParticularWordList removeObjectForKey:self.theKey];

but somehow, self.thisParticularWordList turned into a non-mutable NSDictionary.

What am I doing wrong?

Sjakelien
  • 2,255
  • 3
  • 25
  • 43

2 Answers2

5

This is because of the copy attribute of your property. Have a look at this answer.

A solution would be to make the property strong and then when assigning it, you would do self.yourProperty = [yourDictionary mutableCopy];.

Or even [NSMutableDictionary dictionaryWithDictionary: someDictionary]; as that also creates a new dictionary.


A whole code example demonstrating the fix:

MyClass.h

@property (strong, atomic) NSMutableDictionary *thisParticularWordList;

MyClass.m

theSubview.thisParticularWordList = [NSMutableDictionary dictionaryWithDictionary: someDictionary];

Then this will work after assigning:

[self.thisParticularWordList removeObjectForKey:self.theKey];
Community
  • 1
  • 1
Dominik Hadl
  • 3,609
  • 3
  • 24
  • 58
  • Thanks! Changing 'copy' into 'strong' did the trick. I have to admit that my knowledge of attributes is wobbly. I'm now studying http://stackoverflow.com/questions/9859719/objective-c-declared-property-attributes-nonatomic-copy-strong-weak – Sjakelien Mar 15 '16 at 09:04
  • @Sjakelien That is a great SO question, be sure to read that. Attributes can make a big impact on your code, inculding memory management, code safety and even thread safety. If you found my answer sufficient, can you please mark it as the correct one? :) – Dominik Hadl Mar 15 '16 at 09:08
  • I know this is not the place for questions, but how come that Xcode gives a lot of helpful hints, but doesn't suggest that I should use 'strong' in this case? Are there situations where you would want to have a 'copy'-attribute to an NSMutableDictionary? – Sjakelien Mar 15 '16 at 19:27
0

you need to be worry about this particular line:

@property (copy, atomic) NSMutableDictionary * thisParticularWordList;

because the property's setter copies your mutable collection and makes it immutable one; that is why you get the crash later.


you may need to consider to keep a strong reference only instead of copying it, like:

@property (strong, atomic) NSMutableDictionary * thisParticularWordList;
holex
  • 23,961
  • 7
  • 62
  • 76
  • This looks like the same answer which was answered 10 minutes before. What's the point of the duplication? – trojanfoe Mar 15 '16 at 09:15
  • it seems to me, the accepted answer copies my actual explanation of the situation. – holex Mar 15 '16 at 09:18
  • Copies it? But it was entered 10 minutes before yours? – trojanfoe Mar 15 '16 at 09:20
  • that answer was a messy _gibberish_ only to me, not a real solution or explanation. – holex Mar 15 '16 at 09:22
  • OK so first you say that it copies your answer (even though it was entered before yours) and now it's gibberish. I have to disagree with both those points. – trojanfoe Mar 15 '16 at 09:23
  • yes, when I posted my answer that was nothing about showing how the property should be defined, and after 3 minutes I posted mine – magically – it appears the same concept in that answer as I presented. – holex Mar 15 '16 at 09:25
  • Wow - please check the edit - all that was added was the code sample; I don't see anything added to the explanation. Anyway nevermind. – trojanfoe Mar 15 '16 at 09:27
  • their explanation is pasted from the linked answer only; I'm not chasing here upvotes but I'm committed to present easily readable and understandable answers for the actual problems; and in my view the answer you referred was nothing like that on any level. anyway, nevermind. – holex Mar 15 '16 at 09:32
  • I am sorry, but your answer isn't in any way more or less readable than mine. I don't see why you think I copied everything from you, as I answered first and there isn't really any other way of declaring a property so it doesn't look the same as in your answer. – Dominik Hadl Mar 15 '16 at 09:39
  • Wow... I'm kind of sorry that I asked this question. – Sjakelien Mar 15 '16 at 19:28
  • @Sjakelien, it is not about you – it is about the answer's quality only. – holex Mar 16 '16 at 09:40