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 815423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:40:51+00:00 2026-05-15T01:40:51+00:00

I want to create a controller that depends on the class of a given

  • 0

I want to create a controller that depends on the class of a given instance of a model

 -(BaseController *)getControllerForModel:(Model *)model
    {
     BaseController *controller = nil;
     Class controllerClass = [BaseController class]; //the default value

        //find the right controller
     if ([model isMemberOfClass:[ModelClass1 class]]) 
      controllerClass = [ConcreteController1 class];
     else if ([model isMemberOfClass:[ModelClass2 class]])
      controllerClass = [ConcreteController2 class];
     else if ([model isMemberOfClass:[ModelClass3 class]])
      controllerClass = [ConcreteController3 class];
...
     else if ([model isMemberOfClass:[ModelClassX class]])
      controllerClass = [ConcreteControllerX class];
     else
      Trace(TRACELEVEL_WARNING, @"Unrecognized model type: %@", NSStringFromClass([model class]));

     //Now instantiate it with the model
     controller = [[[controllerClass alloc] initWithModel:model] autorelease];
     return slotController;
    }

I want to find a more flexible solution to this and thought of having a dictionary, which maps Model-Classes to Controller-Classes and then NSClassFromString could give me the right instance.

My question is this: Is NSClassFromString using much of my applications performance if i use it several times (say, 100 times at once)? Or would it be about as fast as the above approach?

  • 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-15T01:40:52+00:00Added an answer on May 15, 2026 at 1:40 am

    A Class is an id, and as such can be added to an NSDictionary. You should try the following:

    mModelToControllerMap = [[NSDictionary alloc] initWithObjectsAndKeys:
       [ConcreteController1 class] , [ModelClass1 class] ,
       [ConcreteController2 class] , [ModelClass2 class] ,
       ...
       nil];
    

    Then later:

    controllerClass = [mModelToControllerMap objectForKey:[modelInstance class]];
    if ( controllerClass ) ...
    else ...
    

    If you make it a mutable dictionary, you can then have controllers register for the models they want instead of forcing the base class to be aware of all derived classes.

    And a direct answer. At worst, NSClassFromString may need to iterate through all classes to find a match. It could also be a dictionary lookup, but the implementation is not documented and should not be relied on. While doing it a few hundred times probably isn’t too bad, there are better ways.

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

Sidebar

Related Questions

I want to create a base controller class that my other controllers will inherit
I want to create a view controller that supports both landscape and portrait orientations,
I want to create a pointcut that matches any method in my Web controller
I am using CakePHP and want to create a URL to a controller/view without
I have controller with action new , and I want it to create ActiveRecord::Base
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
Can I create a Controller that simply returns an image asset? I would like
What I'm looking to do is create a custom controller list that displays a
I have several types os root view controller that I want to instantiate one

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.