Reputation: 4127
I have some NSDictionarys
full of data and I have a method which utilizes the data. Which NSDictionary
is used depends on some input from the user, so I was wondering how I could create a pointer to the NSDictionary
with the data.
For example
The user chooses "option foo", so the method will need to use the "foo dictionary". Instead of copying the "foo dictionary" into a temporary dictionary, how can I reference it so that "tempdictionary" has the contents of "foo dictionary" for example.
That probably made no sense, but thanks in advance.
Upvotes: 0
Views: 498
Reputation: 8667
Copying doesn't happen by default. Just grab the NSDictionary
you want and assign it to a new pointer:
NSDictionary *fooDictionary = [dataDictionary valueForKey:@"foo"];
That will give you a pointer to the dictionary stored within dataDictionary
. It doesn't make a copy unless you ask it to.
Upvotes: 5
Reputation: 49354
The copy isn't made unless you explicitly tell the code to do so. E.g.:
NSDictionary *fooDict = ...; // initialize fooDict and probably other dicts
if ([self useFoo]) {
[self stuffWithFooDict:fooDict]; // will pass pointer of fooDict
}
if ([self useFooCopy]) {
[self stuffWithFooCopy:[fooDict copy]]; // will use a copy of fooDict
}
Upvotes: 1