Joe Booth
Joe Booth

Reputation: 501

Does adding a NSMutableDictionary to a NSMutableDictionary make a copy or does it keep a pointer

Does adding a NSMutableDictionary to a NSMutableDictionary make a copy or does it keep a pointer.

I want to be able to update the content of the first NSMutableDictionary and have that tracked within the other NSMutableDictionary - for example...

    NSMutableDictionary* dictA = [[NSMutableDictionary alloc] init];
    NSMutableDictionary* dictB = [[NSMutableDictionary alloc] init];
    [dictA setValue:[NSNumber numberWithInt:1 ] forKey:@"number"];
    [dictB setValue:dictA forKey:@"dictA"];

    [dictA setValue:[NSNumber numberWithInt:2 ] forKey:@"number"];

Will dictB.dictA.number now = 2?

Upvotes: 0

Views: 65

Answers (1)

user529758
user529758

Reputation:

Yes, it will. In general, Cocoa collection classes only retain the objects added to them (the purpose: this way you can insert non-copyable objects too).

(With CoreFoundation trickery and toll-free bridging, it is possible to realize an NSArray that copies its elements, though.)

Upvotes: 2

Related Questions