0

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.