0

In C# you can create a delegate method, assign it to a variable or pass it into a method as if it were a variable. For example:

public delegate int Lookup(String s);
//...
public static int Evaluate(String exp, Lookup variableEvaluator)
{
//...
}

I heard that in C you can create a pointer to any method and then pass that pointer to a method. Can anyone give me a simple example of doing that in Objective-C? Of course, I can create an object with a singe method and pass that object into a method. But I am curious if there is a way of doing that similar to that of C# or C.

3 Answers 3

2

Lots of ways.

One: the good. Use blocks (closures, lambda calculus, however you call it):

typedef void (^MyCallback)();

- (void)callTheCallback:(MyCallback)blockToInvoke
{
    blockToInvoke();
}

MyCallback cb = ^{
    NSLog(@"I was called! :D");
};
[self callTheCallback:cb];

Two: the bad. Grab a pointer to the method function itself and call that. (Warning: if you use this approach, I'll sue you.)

- (void)callTheCallback:(IMP)funcPtrToCall withObject:(id)obj selector:(SEL)sel
{
     funcPtrToCall(obj, sel);
}

- (void)someCallbackMethod
{
    NSLog(@"I was called! :D");
}

IMP implemt = [[self class] instanceMethodForSelector:@selector(someCallbackMethod)];
[self callTheCallback:implemt withObject:self selector:@selector(someCallbackMethod)];

Three: the ugly. Use a delegate:

- (void)delegateMethodOfSomeObject:(SomeObject *)obj
{
    NSLog(@"I was called! :D");
}

SomeObject *obj = [[SomeObject alloc] init];
obj.delegate = self;
[obj makeThisObjectSomehowCallItsDelegateThatIsCurrentlySelf];
Sign up to request clarification or add additional context in comments.

Comments

1

Two quick thoughts come to mind.

The short answer is called "blocks", but it's lower level than is probably recommended for what you need.

The "cleaner" solution (read: higher level) is to pass two params: and object (called "target") and a selector (called "action"). This is a very common pattern in Objective-C, so I'll only demonstrate this one. If you are interested in the blocks idea, check out this doc.

Essentially, the object should be passed as an id, and the selector as a SEL, for which we have the handy @selector() construct:

-(void) doThingWithTarget:(id) targetObj action:(SEL) actionSel {
  if([targetObj respondsToSelector:actionSel]) {
    [targetObj performSelector:actionSel withObject:self];
  }
}

// ...
[thatOtherObject doThingWithTarget:self action:@selector(myMethod:)];

// ... where

-(void) myMethod:(id) sender {
  // sender is the calling object, or should be by contract.
}

3 Comments

Most target/selectors in Cocoa have been steadily moving towards blocks. Blocks simplify several inherent problems in target/selector (particularly under ARC). While target/selector is a venerable and useful approach, going back to the NeXT days, I do not generally recommend it for new code. For most problems, blocks are a better solution. The big advantage of target/selector is that they can be serialized (unlike blocks), and so are ideal for nib files.
Target-action is by no means "higher-level". They're using the Objective-C runtime just like blocks do. In fact, blocks are a more elegant solution for having callbacks, event handlers et al...
@RobNapier I like the comment, thanks. Note that blocks have their own ARC implications going the other way (e.g. what does the block have a strong pointer to via it's closure-like mechanism?). That certainly doesn't preclude one from choosing them over target/action; and your comment alone prompts me to improve my disposition toward them (or to remember that I was growing more affectionate when I slowed the amount of Objective-C I was doing 2 months ago).
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.