Question

I'm new to objective c and I've got an interesting situation where I need two different NSDictionary objects that point to the same values. In that situation should I use strong or weak in the property declaration? Or should I do strong in one and weak in the other?

In Game.m

@property (strong/weak, nonatomic) NSDictionary* answers1;

In User.m

@property(strong/weak, nonatomic) NSDictionary* answers2;

In both cases the key will be an integer, but the value will be an answer object of my own making. Both answers1 and answers2 will need to exists for roughly the same amount of time. When it comes time to get rid of one it'll be okay to get rid of the other.

Était-ce utile?

La solution

Both should probably be strong. Each class should do its own memory management without having to worry about what other classes are doing. Therefore, each should keep its own strong reference.

Autres conseils

In this case, the best would actually be copy. This way, in addition to retaining the dictionary, you will create an immutable copy of the one passed to you, ensuring the dictionary does not get modified from an outside influence (for example, passing a mutable dictionary which can later mutate).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top