Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C's "obj performSelector" vs objc_msgSend( )?

Going through Apache Cordova's source code, I ran into two lines of code that I'm puzzled about:

    //[obj performSelector:normalSelector withObject:command];
    objc_msgSend(obj,normalSelector,command);

From Apple's documentation, there doesn't seem to be a lot of difference between these two methods.

id objc_msgSend(id theReceiver, SEL theSelector, ...) Sends a message with a simple return value to an instance of a class.

- (id)performSelector:(SEL)aSelectorwithObject:(id)anObject Sends a message to the receiver with an object as the argument. (required)

What exactly is the difference between these two methods? In the case above, both are sending messages with an object as an argument to a receiving object.

like image 708
nsg Avatar asked Sep 03 '12 03:09

nsg


2 Answers

You're asking the difference between two "methods" but only one of them is actually a method. The objc_msgSend function is, well, a function. Not a method.

The objc_msgSend function is the function that you actually call when you invoke any method on any object in Objective C. For example, the following two are basically equivalent:

// This is what the compiler generates
objc_msgSend(obj, @selector(sel:), param);

// This is what you write
[obj sel:param];

// You can check the assembly output, they are *almost* identical!

The major difference here is that objc_msgSend does not get type checked by the compiler -- or at least, its arguments don't get type checked against the selector's parameter types. So the following are roughly equivalent:

[obj performSelector:normalSelector withObject:command];
objc_msgSend(obj, @selector(performSelector:withObject:),
             normalSelector, command);

But, that's a bit of a waste, since all performSelector:withObject: does is call objc_msgSend.

HOWEVER: You should stay away from obc_msgSend because it is not type-safe, as mentioned above. All the apache devs are doing is removing a single method call, which will only give you very slight performance benefits in most cases.

like image 144
Dietrich Epp Avatar answered Oct 11 '22 13:10

Dietrich Epp


The commented out line is correct, the objc_msgSend() line is incorrect in that it needs to be explicitly typed (varargs are not compatible with non-varargs function calls on some platforms sometimes).

Effectively they do the same thing. Really, the method call version is just a wrapper around objc_msgSend().

like image 34
bbum Avatar answered Oct 11 '22 12:10

bbum