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

  • Home
  • SEARCH
  • 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 6809313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:04:16+00:00 2026-05-26T20:04:16+00:00

I have a fairly lengthy if statement. The if statement examines a string type

  • 0

I have a fairly lengthy if statement. The if statement examines a string “type” to determine what type of object should be instantiated. Here’s a sample…

if ( [type rangeOfString:@"coin-large"].location != NSNotFound ) 
{
   ... create large coin ...
   mgr = gameLayer.coinLargeMgr;
}
else if ( [type rangeOfString:@"coin-small"].location != NSNotFound ) 
{
   mgr = gameLayer.coinLargeMgr;
}

... more else statements ...

myObject = [mgr getNewObject];

The “else-if” statements continue for other object types which stand at about 20 right now and that number is likely to increase. This works quite well but in terms of maintenance and efficiency I think it could be improved. My leading candidate right now is to create an NSDictionary keyed on the object type string (coin-small, coin-large, etc.) and with the value of the manager object that should be tied to that type. The idea being that this would be a quick look for the type of object I need to create. Not sure this is the best approach, continuing to look at other options but am curious what folks here might have done for a similar problem. Any help/feedback is greatly appreciated.

  • 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-26T20:04:17+00:00Added an answer on May 26, 2026 at 8:04 pm

    The dictionary approach would be easily doable. Assuming the various managers have been boiled down to specific instances when you create the dictionary, it’d be just the same as almost any object-oriented language:

    NSDictionary *stringsToManagers = 
        [NSDictionary dictionaryWithObjectsAndKeys:
             @"coin-large", gameLayer.coinLargeMgr,
             @"coin-small", gameLayer.coinSmallMgr,
             nil];
    
    // this is assuming that type may contain multiple types; otherwise
    // just use [stringsToManagers objectForKey:string]
    for(NSString *string in [stringsToManagers allKeys])
    {
        if([type rangeOfString:string].location != NSNotFound)
        {
             [[stringsToManagers objectForKey:string] addNewObject];
             // or get it and store it wherever it should go
        }
    }
    

    If all the managers do is vend appropriate objects, the more object-oriented approach might be:

    NSDictionary *stringsToClasses = 
        [NSDictionary dictionaryWithObjectsAndKeys:
             @"coin-large", [LargeCoin class],
             @"coin-small", [SmallCoin class],
             nil];
    
    // this is assuming that type may contain multiple types; otherwise
    // just use [stringsToManagers objectForKey:string]
    for(NSString *string in [stringsToManagers allKeys])
    {
        if([type rangeOfString:string].location != NSNotFound)
        {
             id class = [stringsToManagers objectForKey:string];
    
             id newObject = [[class alloc] init];
             // this is exactly the same as if, for example, you'd
             // called [[LargeCoin alloc] init] after detecting coin-large
             // within the input string; you should obviously do something
             // with newObject now
        }
    }
    

    That could save you having to write any managers if your program structure otherwise fits.

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

Sidebar

Related Questions

We have a fairly lengthy build process, and so its nice to be able
I have an existing, fairly lengthy, SQL query. I would like to select records
We have fairly large C++ application which is composed of about 60 projects in
I have a fairly small MySQL database (a Textpattern install) on a server that
I have a fairly simple ASP.NET 2.0 menu control using a sitemap file and
I have a fairly standards compliant XHTML+CSS site that looks great on all browsers
I have a fairly large codebase that depends on MooTools v1.11 and am about
I have a fairly good feel for what MySQL replication can do. I'm wondering
I have a fairly simple const struct in some C code that simply holds
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have

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.