简体   繁体   中英

Pointers in objective-c

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.

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.

The copy isn't made unless you explicitly tell the code to do so. Eg:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM