• 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: default method on an object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: default method on an object


  • Subject: Re: default method on an object
  • From: Greg Titus <email@hidden>
  • Date: Fri, 6 Jul 2001 12:37:05 -0700

On Friday, July 6, 2001, at 09:24 AM, Jeff Medcalf wrote:

Is there a way in Cocoa, to declare a method, such that a message to the object containing that method, which does not find a method to invoke, will invoke that method instead. In other words, if I have a class with 1 method, myMethod:, and it gets a message like [myObject someOtherMethod:], is there a way that I can have myMethod: invoked instead of an error being generated?

Yes, this is one of the great features of Objective-C that lets you implement things like Distributed Objects in a very straightforward way. Read the documentation for the -forwardInvocation: method on NSObject, and look at the documentation for NSInvocation. Your object should implement something like:

- (void)forwardInvocation:(NSInvocation *)invocation
{
// change the method from whatever it was to "myMethod:" and then resend it
[invocation setSelector:@selector(myMethod:)];
[invocation invoke];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *result;

// If we don't recognize a method, return the signature for "myMethod:" instead
result = [super methodSignatureForSelector: aSelector];
if (!result)
result = [super methodSignatureForSelector:@selector(myMethod:)];
return result;
}

Hope this helps,
--Greg


References: 
 >default method on an object (From: Jeff Medcalf <email@hidden>)

  • Prev by Date: How to stop initial window ?
  • Next by Date: Re: Drawing text with specifying font
  • Previous by thread: Re: default method on an object
  • Next by thread: Re: default method on an object
  • Index(es):
    • Date
    • Thread