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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:15:44+00:00 2026-06-18T04:15:44+00:00

Is this the best way to write this if else code checking? // set

  • 0

Is this the best way to write this if else code checking?

// set the default to Italy if countrySaved is null
if (lang == NULL) {
    lang = @"it";
}

// otherwise set the correct lang based on the country chosed and saved in nsuserdefaults
if ([countrySaved isEqual:@"Brazil"]) {
    lang = @"br";
}

else if ([countrySaved isEqual:@"Spain"]) {
    lang = @"es";
}

else if ([countrySaved isEqual:@"Italy"]) {
    lang = @"it";
}

else if ([countrySaved isEqual:@"United States"]) {
    lang = @"us";
}

else if ([countrySaved isEqual:@"United Kingdom"]) {
    lang = @"uk";
}

thanks for any help

  • 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-18T04:15:45+00:00Added an answer on June 18, 2026 at 4:15 am

    Store the language IDs in a dictionary:

    NSDictionary *languageIDs = @{
        @"Brazil" : @"br",
        @"Spain" : @"es",
        @"Italy" : @"it",
        @"United States" : @"us",
        @"United Kingdom" : @"uk"
    };
    

    Then simply assign lang from said dictionary:

    lang = languageIDs[countrySaved];
    
    if (!lang) {
        lang = languageIDs[@"Italy"];
    }
    

    Or the shorter way I’d do it using a ternary operator:

    lang = languageIDs[countrySaved] ?: languageIDs[@"Italy"];
    

    Also you should probably either load them from a plist file or use NSString consts in order to avoid eventually redundant and by that unmaintainable literals scattered all over your code.

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

Sidebar

Related Questions

What is the best way to write this function ontop of jQuery. Background: I'm
What is the best way to write a no-op statement in Delphi? Take this
What would be the best way to optimize this code below to make it
This question ( Best way to strip punctuation from a string in Python )
Is this the best way to convert String hex to bytes? Or can you
This question is related to this one: Best way to store product colors in
Okay I'll try describe this the best way I can. I have a chat
Ok so let me try to explain this the best way that i can.
I am not sure if I am going about this the best way, but
This is the best way I can think of phrasing this question, given this

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.