Search code examples
objective-cselectornsnumber

performSelector causes leak because not right type in objective c


I have the following code:

SEL moveAlongBoth = @selector(moveAlongX:andY:);
if ([p1 respondsToSelector:moveAlongBoth]) {
    [p1 performSelector: moveAlongBoth
             withObject: [NSNumber numberWithInt:1]
             withObject: [NSNumber numberWithInt:1]];
}

I am getting a "performSelector may cause leak" warning. But

[p1 moveAlongX:1 andY:1];

Works just fine.

I understand that I am getting the error because the values are set to (int) in the implementation and I am using NSNumber. Without changing the implementation, how would I go about declaring a number value to int (if possible)?


Solution

  • Why can't you just do this:

    if ([p1 respondsToSelector:@selector(moveAlongX:andY:)]) {
        [(id)p1 moveAlongX:1 andY:1];
    }
    

    By the way, the Cocoa naming convention would have you call this method moveAlongX:y:.