1

I have some variables, which cache some data from a webservice.

To make my code more dynamic, I want to return a double pointer to the cache-variable. So it's a double pointer. I have some issues doing this with ARC.

Here's what I got:


- (id *)pointerToSectionCacheProperty:(SomeSection)section {
    switch (section) {
        case Section1:
        {
            return &_section1Cache;
        }
            break;
        case Section2:
        {
            return &_section2Cache;
        }
            break;
        case Section3:
        {
            return &_section3Cache;
        }
            break;
    }

    return nil;
}

ARC gives me the following error:

Returning 'NSArray *__strong *' from a function with result type '__autoreleasing id *' changes retain/release properties of pointer

Is this the wrong approach?

If so, what is the right approach?

IluTov
  • 6,807
  • 6
  • 41
  • 103
  • What purpose does the double pointer serve? ARC is simply confused, function results are autorelease by default, what does your NSArray * have as its declarations? – ImHuntingWabbits Sep 17 '13 at 18:10
  • @ImHuntingWabbits It's simply `strong`variable – IluTov Sep 17 '13 at 18:12
  • 1
    I believe the correct decl would be (strong NSArray **) since the caller will effectively have to manage that object on their own if they change the value. – ImHuntingWabbits Sep 17 '13 at 18:16
  • @ImHuntingWabbits You were very close :) Added the solution – IluTov Sep 17 '13 at 18:26
  • Hah I really should learn ARC. You should also mark it as answered. – ImHuntingWabbits Sep 17 '13 at 18:47
  • @ImHuntingWabbits That's only possible in 2 days... – IluTov Sep 17 '13 at 18:49
  • See also [this answer](http://stackoverflow.com/questions/8814718/handling-pointer-to-pointer-ownership-issues-in-arc/8829294#8829294) - it is not exactly the same situation, as it is directed at passing pointers to pointers *into* methods and not receiving them *from* methods, but the same principles apply. HTH. – CRD Sep 17 '13 at 19:40

1 Answers1

1

Solution


Got it working like this:


- (NSArray *__strong *)pointerToSectionCacheProperty:(SomeSection)section {
    switch (section) {
        case Section1:
        {
            return &_section1Cache;
        }
            break;
        case Section2:
        {
            return &_section2Cache;
        }
            break;
        case Section3:
        {
            return &_section3Cache;
        }
            break;
    }

    return nil;
}

As a sidenote, - (id __strong *)... will work just as well.

IluTov
  • 6,807
  • 6
  • 41
  • 103