Silver Challgenge: performSelector: vs forwardInvocation:


#1

Hey folks-

I’m struggling with the Silver Challenge. I made a simple Base Cell class that works generically, but I wanted to extends its functionality by creating something more dynamic: a method that forwards an array arguments (up to 2 max). I had it all set up and ready to rock - EXCEPT when one of the arguments was a selector (as is the case in this chapter/challenge); NSArray won’t take selectors in their raw form. Boo. :frowning:

So, I started hunting down a way to accomplish this and came across NSMethodSignature and NSInvocation, which led me to forwardInvocation:. This combination of classes & methods seems perfectly suited to accomplish the type of work we’re doing in the latter half of this chapter (although to be fair I can’t get the code working how I like). But is it?

My question: why don’t we use these classes and methods for this purpose? If not for this purpose, why do those methods and classes exist? Why is performSelector:withObject:withObject: better than forwardInvocation:?

Here are some links for reference…:

NSInvocation
http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html

NSMethodSignature
http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMethodSignature_Class/Reference/Reference.html


#2

ok, did some more searching, and found this note in Apple’s developer docs:

…does that mean that methods that begin with - (id)methodName would use performSelector:, while say - (int)nonObjectMethodName would use forwardInvocation:?

also what about methods that return i[/i]? or methods that return non-id objects, e.g. i[/i]?

note: for the curious, i also posted this question on StackOverflow: http://stackoverflow.com/questions/11379525/objective-c-performselector-vs-forwardinvocation