iosobjective-cperformselector

Any danger using performselector in objective-c?


I've been stuck for a couple of days with a crash on Crashlytics that says :

libobjc.A.dylib

objc_msgSend

I managed to point out what seems to be the reason of this crash : __NSThreadPerformPerform

With a little search on stack it seems to be linked with performSelector:withObject:afterDelay:

The code I have mentions a delay of 0 all the time. I use this method to call out what we could call "functors" in iOS, by recreating selectors dynamically from an array of strings that I access from a enum state.

And basically it's all the informations I have from this so far, I learned that the afterDelay: makes your call delayed to the next RunLoop. Does it applies as well on performSelector:withObject: or the afterDelay: adds something to the method that causes this?

And, finally, the real question :

Is performSelector:withObject:afterDelay: a good way to make functors, or is it dangerous to use it like this, and should I be doing any other way?

Thanks in advance,


Solution

  • Don't just assume that your object still exists and can perform selector. Test it before!

    if ([someObject respondsToSelector:@selector(someSelector)]) {
       [someObject performSelector:@selector(someSelector) withObject:nil];
    }