Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4249738
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:26:11+00:00 2026-05-21T04:26:11+00:00

Are there any techniques for emulating traits or mixins in Objective-C? In Scala, for

  • 0

Are there any techniques for emulating traits or mixins in Objective-C?

In Scala, for example, I can do something like this:

trait ControllerWithData {
  def loadData = ...
  def reloadData = ...
  def elementAtIndex = ...
}

trait ControllerWithStandardToolbar {
  def buildToolbar = ...
  def showToolbar = ...
  def hideToolbar = ...
}

class MyTableController extends ControllerWithData 
                        with ControllerWithStandardToolbar {
  def loadView = {
     super.loadView

     loadData
     buildBar
  }
}

It’s basically a way to combine (or mix in) multiple pieces of functionality into a single class. So right now I have kind of an all-purpose UIViewController that all of my controllers subclass from, but it would be neater if I could break that down and have specific controllers inherit specific behavior.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:26:12+00:00Added an answer on May 21, 2026 at 4:26 am

    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 “-doFoo” and “-doBar“, respectively. You could define your class to have traits, like this:

    @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
    

    Now, you can create instances of MyClassWithTraits, and add whatever “trait” objects you’d like:

    MyClassWithTraits *widget = [[MyClassWithTraits alloc] init];
    [widget addTrait:[[[Foo alloc] init] autorelease]];
    [widget addTrait:[[[Bar alloc] init] autorelease]];
    

    You could make these calls to -addTrait: in MyClassWithTraits’ -init 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.

    And then you can call -doFoo and -doBar as if they were implemented by widget, even though the messages are being forwarded to one of its trait objects:

    [widget doFoo];
    [widget doBar];
    

    (Edit: Added error handling.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any documented techniques for speeding up mySQL dumps and imports? This would
Are there any techniques/proposals to enforce unique constraints? Yes, we can create key that's
What are, exactly mixins? Are there any techniques to achieve that in c#?
Is there any query which can return me the number of revisions made to
Is there any efficiency difference in an explicit vs implicit inner join? For example:
Are there any techniques to optimize code in order to ensure lesser power consumption.Architecture
I am wondering if there are any techniques to identify a web crawler that
Is there any way you can speed up XSL transformations? I am using the
First is there any technique for using Action Script for recording sound from microphone?
Is there any way to check whether a file is locked without using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.