Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's no direct language support, but you could accomplish something similar with message forwarding. Let's say you have trait classes "Foo" and "Bar", which define methods "<code>-doFoo</code>" and "<code>-doBar</code>", respectively. You could define your class to have traits, like this:</p> <pre><code>@interface MyClassWithTraits : NSObject { NSMutableArray *traits; } @property (retain) NSMutableArray* traits; -(void) addTrait:(NSObject*)traitObject; @end @implementation MyClassWithTraits @synthesize traits; -(id)init { if (self = [super init]) { self.traits = [NSMutableArray array]; } return self; } -(void) addTrait:(NSObject*)traitObject { [self.traits addObject:traitObject]; } /* Here's the meat - we can use message forwarding to re-send any messages that are unknown to MyClassWithTraits, if one of its trait objects does respond to it. */ -(NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector { // If this is a selector we handle ourself, let super handle this if ([self respondsToSelector:aSelector]) return [super methodSignatureForSelector:aSelector]; // Look for a trait that handles it else for (NSObject *trait in self.traits) if ([trait respondsToSelector:aSelector]) return [trait methodSignatureForSelector:aSelector]; // Nothing was found return nil; } -(void) forwardInvocation:(NSInvocation*)anInvocation { for (NSObject *trait in self.traits) { if ([trait respondsToSelector:[anInvocation selector]]) { [anInvocation invokeWithTarget:trait]; return; } } // Nothing was found, so throw an exception [self doesNotRecognizeSelector:[anInvocation selector]]; } @end </code></pre> <p>Now, you can create instances of MyClassWithTraits, and add whatever "trait" objects you'd like:</p> <pre><code>MyClassWithTraits *widget = [[MyClassWithTraits alloc] init]; [widget addTrait:[[[Foo alloc] init] autorelease]]; [widget addTrait:[[[Bar alloc] init] autorelease]]; </code></pre> <p>You could make these calls to <code>-addTrait:</code> in MyClassWithTraits' <code>-init</code> method, if you want every instance of that class to have the same kind of traits. Or, you could do it like I've done here, which allows you to assign a different set of traits to each instance.</p> <p>And then you can call <code>-doFoo</code> and <code>-doBar</code> as if they were implemented by widget, even though the messages are being forwarded to one of its trait objects:</p> <pre><code>[widget doFoo]; [widget doBar]; </code></pre> <p>(<strong>Edit</strong>: Added error handling.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload