• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: question about selector as argument
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: question about selector as argument


  • Subject: Re: question about selector as argument
  • From: Sherm Pendley <email@hidden>
  • Date: Sat, 18 Jun 2005 11:17:28 -0400

On Jun 18, 2005, at 9:34 AM, Ivan S. Kourtev wrote:

I was able to get this to work by modifying the callback to take an NSNumber* argument, then using performSelector:withObject within but doSomething. But I am sure there must be another way to do what I want.

...

- (void)doSomething:(id)anObject withCallBack:(SEL)aSelector {
    ...
    [anObject aSelector:10];
    ...
}

As you've discovered, this doesn't work - it simply looks for a method named aSelector:. And, as you've also found, performSelector:withObject:, as its name implies, only works with object arguments. What's left is NSInvocation. You can use that to build up any argument list and pass it. For instance:


- (void)doSomething:(id)anObject withCallBack:(SEL)aSelector {
NSMethodSignature* sig = [anObject methodSignatureForSelector:aSelector];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];


    int theArg = 10;

    [inv setTarget:anObject]; // "Hidden" arg 0, self
    [inv setSelector:aSelector]; // "Hidden" arg 1, _cmd

    [inv setArgument:&theArg atIndex:2];

    [inv invoke];
}

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: question about selector as argument
      • From: "Ivan S. Kourtev" <email@hidden>
References: 
 >question about selector as argument (From: "Ivan S. Kourtev" <email@hidden>)

  • Prev by Date: Re: question about selector as argument
  • Next by Date: File system case sensitivity
  • Previous by thread: Re: question about selector as argument
  • Next by thread: Re: question about selector as argument
  • Index(es):
    • Date
    • Thread