P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

What is @selector directive in terms a C# developer can comprehend?

What is the purpose of using a selector here?

CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)];

Source: Code from a beginner OpenGL es 2.0 site

I read the definition

The @selector() directive lets you refer to the compiled selector, rather than to the full method name.

Unfortunetly, this does not clear it up for me. My expertise is in C#. So I appreciate it if you can relate the answer to how a similar solution might be achieved in C#.

Upvotes: 1

Views: 543

Answers (2)

Richard J. Ross III
Richard J. Ross III

Reputation: 55533

A selector declares the name of a function. That's it. It's very similar to the Reflection class MethodInfo, but it's much simpler to use.

Comparison of C# and Objective-C: Notice the C# code may be a bit off, as I haven't worked with it in a long time

// C#
using namespace system.reflection;

class someClass {
     void someMethod(object input) {
           string methodName = "doSomething";

           input.getType().getMethod(methodName).invoke(input, new Object[] { });
     }
}

// OBJC
@implementation someClass 

-(void) someMethod:(id) input
{
     SEL methodName = @selector(doSomething);

     [input performSelector:methodName];
}

@end

As far as the internals of a SEL are concerned, it is a C-string that has been put into a private map for speed in lookup at runtime.

Upvotes: 6

Alec
Alec

Reputation: 1706

Here are two links that together explain them pretty well. The first is Apple's documentation on selectors and the second is about the difference between selectors, delegates, and blocks (which are comparatively new).

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocSelectors.html

http://bradcupit.tumblr.com/post/3431169229/ios-blocks-vs-selectors-and-delegates

EDIT:

Oh, and not C#, but if you are familiar with javaScript or the like, selectors are similar to callbacks. Again, that second post helps explain the uses/similarities/differences.

Upvotes: 0

Related Questions