2

I am trying to have a fairly dynamic api for the level class of my game... basically, I just have a bunch of class methods optionsForLevel1, optionsForLevel2, ...etc, that all return a dictionary object with things like how much time the level should have, how many bonus points, the level's name, etc...

In my actual game object, when it's time to advance levels, it calls a method on the level object which does:

+(NSDictionary*)performClassSelectorForLevel:(int)identifier {
    SEL sel = NSSelectorFromString([NSString stringWithFormat:@"optionsForLevel%d", identifier]);
    return [self performSelector:sel];
}

This gives me a warning: PerformSelector may cause a leak because its selector is unknown.

...

How can I resolve this warning?

patrick
  • 9,290
  • 13
  • 61
  • 112
  • possible duplicate of [PerformSelector warning](http://stackoverflow.com/questions/8773226/performselector-warning) – Daniel Aug 22 '12 at 00:55

2 Answers2

2

This is interesting. You can't. Not in my experience. Simply this is a warning, not an error, this "may" cause a leak.

When using performSelector: it's your responsibility to make sure it doesn't leak, of course the compiler doesn't know the selector in the NSString, it's unknown at compile time, as it will have its value assigned at runtime.

You can suppress this warning, but it's okay to ignore

Check out this answer for more details: PerformSelector warning

Community
  • 1
  • 1
Daniel
  • 23,129
  • 12
  • 109
  • 154
  • 2
    It isn't OK to ignore the warning. While it might seem pedantic, the compiler is correct in that it can't *know* that a leak isn't caused by that call. It is a better long term strategy to eliminate use of `performSelector:` and variants. – bbum Aug 22 '12 at 03:09
0

The warning is generated by the compiler because ARC needs to know what kind of objects may be returned by that method to make sure memory is not mismanaged.

More details on this here: performSelector may cause a leak because its selector is unknown.

HepaKKes
  • 1,555
  • 13
  • 22