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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:03:29+00:00 2026-06-08T12:03:29+00:00

I have a command list includes hundreds of commands, and this command list needs

  • 0

I have a command list includes hundreds of commands, and this command list needs to be called frequently. like:

if([command isEqualToString:"openPage1"]){
  open page 1
}else if ([command isEqualToString:"jumpToPage4"]){
  get param1 and param2
  jump to page 4
}else if ([command isEqualToString:"backToPage10"]){
  get param1
  back to page 10
}....

Since there are hundreds of commands and called frequently, so I don’t think “if else” is a good way…

Which algorithm is faster and efficiency?

  • 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-06-08T12:03:31+00:00Added an answer on June 8, 2026 at 12:03 pm

    You could have an NSDictionary that maps the command names directly to code, be it selectors, invocations or blocks. Something like:

    NSMutableDictionary *actions = [NSMutableDictionary dictionary];
    [actions setObject:^{
        [self getParam1];
        [self getParam2];
        [self navigateSomewhere];
    } forKey:@"openPage1"];
    

    And then:

    dispatch_block_t action = [actions objectForKey:command];
    if (action) {
        action();
    } else {
        /* handle unknown command */
    }
    

    Of course the dictionary would be initialized just once and then cached. If the action is always the same call, just with different arguments, you can map the command names directly to the arguments:

    // setup:
    NSDictionary *commandsToPages = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:1], @"command1",
        /* more mappings */,
        nil];
    // …and later:
    NSNumber *pageNumber = [commandsToPages objectForKey:commandName];
    [self displayPage:[pageNumber intValue]];
    

    And there’s also the option of just parsing the command name to extract the page number, if that’s possible.


    PS. Starting with LLVM 4.1 (?) you can also use the shorthand literal syntax to create the action dictionary, which makes it a bit easier on the eyes:

    NSDictionary *actions = @{
        @"command1" : ^{
            …
        },
        @"command2" : ^{
            …
        },
    };
    

    Note that even the trailing comma after the second command block works.

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

Sidebar

Related Questions

I have a regex which includes a list of commands. But I don't know
I have a list of data that includes both command strings as well as
I have a long command saved in a file. I also have a list
I currently have two SQL commands. One retrieves a list of unique IDs from
I have read the other posts, e.g., vim: Executing a list of editor commands
I have this command to go to every directory and once there run some
I have an ant task that executes some command on a list of files.
Hi i have this command pattern example from the web , but there is
I have some XML data which includes URIs. I want to list the URIs
For security reasons (I'm a developer) I do not have command line access to

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.