If I have something like this:
SomeObject *obj = [[SomeObject alloc] init];
obj.someIvar = 100;
NSMuteableArray *arr = [[NSMutableArray alloc] initWithCapacity:10];
[arr addObject:obj];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:50];
[dict setValue:obj forKey:@"key"];
[obj release];
Can I update obj like so:
SomeObject *objFromDict = [dict objectForKey:@"key"];
objFromDict.someIvar = 5200;
…and expect the object in arr to be updated as well? I’m assuming collections are storing and giving out pointers.
Yes, you are right.
In
NSMuteableArray *arrandNSMutableDictionary *dictwill be stored reference to objectSomeObject *obj. When you are calling[dict objectForKey:@"key"];you get this reference and in expressionobjFromDict.someIvar = 5200;you are modifyingpropertysomeIvar.When you will try to get the same object from
arrthe value ofsomeIvarwill be also changed because of arrays and dictionaries just store references to instances.