• 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: NSInvocation help
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSInvocation help


  • Subject: Re: NSInvocation help
  • From: Marcel Weiher <email@hidden>
  • Date: Fri, 28 Dec 2001 17:18:43 +0100

On Freitag, Dezember 28, 2001, at 02:31 Uhr, Gregory Weston wrote:

Does anyone know of a short and sweet example of how to use NSInvocation? I
grabbed some sample code - can't remember where from - that almost works except
for one oddity. The method I've specified gets invoked on the object I've
specified, but the argument I'm passing (in index 2) is being delivered as if I
sent the object's class instead of the object itself.

Actually there is a bug in your code that essentially puts a random value there, except that in this case the 'random' value is the class.

Given t ( a member of a class that response to useFoo:)
and sel ( = @selector(useFoo:))

{
Foo* foo = [someFactoryObject gimmeOneFoo];
NSMethodSignature* s = [[t class] instanceMethodSignatureForSelector:sel];
NSInvocation* i = [NSInvocation invocationWithMethodSignature];
[i setSelector:s];
[i setTarget:t];
[i setArgument:foo atIndex:2];

NSInvocation takes a pointer to the value, not the value itself. So this needs to be:

[i setArgument:&foo atIndex:2];

Since all classes have the isa-pointer as their first instance variable, you get the class instead.

Marcel

--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.


References: 
 >NSInvocation help (From: Gregory Weston <email@hidden>)

  • Prev by Date: Re: TextField Delegates
  • Next by Date: Input in TextView
  • Previous by thread: NSInvocation help
  • Next by thread: NSTimer Bug?
  • Index(es):
    • Date
    • Thread