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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:12:29+00:00 2026-06-18T08:12:29+00:00

Possible Duplicate: How could I write this if else code checking in a better

  • 0

Possible Duplicate:
How could I write this if else code checking in a better way?

My conditional code here seems repetitive and long. Is there a better approach? I want to test for a string value in a NSDictionary object and then depending upon the value prefix a UILabel with $, £, ¥ currency symbols.

Here’s my code (I’ve just shown 2 examples below, I have more currencies and the code is very long):

  if ([[item objectForKey:@"currency"] isEqualToString:@"EUR"]) {

        NSString *priceConvertToStr = [NSString stringWithFormat:@"€%@", [[item objectForKey:@"price"]stringValue]];

        NSString *priceStringFix = [priceConvertToStr
                                    stringByReplacingOccurrencesOfString:@"(null)" withString:@""];

        priceLabelText.text = priceStringFix;
        [imgView2 addSubview:priceLabelText];

    }

    if ([[item objectForKey:@"currency"] isEqualToString:@"GBP"]) {
        NSString *priceConvertToStr = [NSString stringWithFormat:@"€%@", [[item objectForKey:@"price"]stringValue]];

        NSString *priceStringFix = [priceConvertToStr
                                    stringByReplacingOccurrencesOfString:@"(null)" withString:@""];

        priceLabelText.text = priceStringFix;
        [imgView2 addSubview:priceLabelText];

    }
    if ([[item objectForKey:@"currency"] isEqualToString:@"USD"]) {
        NSString *priceConvertToStr = [NSString stringWithFormat:@"$%@", [[item objectForKey:@"price"]stringValue]];

        NSString *priceStringFix = [priceConvertToStr
                                    stringByReplacingOccurrencesOfString:@"(null)" withString:@""];

        priceLabelText.text = priceStringFix;
        [imgView2 addSubview:priceLabelText];

    }

thanks so much 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-18T08:12:30+00:00Added an answer on June 18, 2026 at 8:12 am

    You can refactor your code to this (possible because essentially 80% of the code within the if statements are identical):

    NSDictionary *currDict = @{
        @"EUR": @"€",
        @"GBP": @"₤",
        @"USD": @"$"
    };
    NSString *currName = [item objectForKey:@"currency"];
    NSString *currency = [currDict objectForKey:currName];
    
    NSString *priceConvertToStr = [NSString stringWithFormat:@"%@%@",
        currency,
        [[item objectForKey:@"price"] stringValue]
    ];
    NSString *priceStringFix = [priceConvertToStr stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
    priceLabelText.text = priceStringFix;
    [imgView2 addSubview:priceLabelText];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: A better way to compare Strings which could be null I have
Possible Duplicate: In C++ why can’t I write a for() loop like this: for(
Possible Duplicate: Best way to call Managed .NET code from Unmanaged code I'm curious
Possible Duplicate: Can you write a block of c++ code inside C#? Is it
Possible Duplicate: Prime Number Formula I am trying to write some code in c#
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25; I am
Possible Duplicate: What does “WARN Could not determine content-length of response body.” mean and
Possible Duplicate: Threads vs Processes in Linux To implement multi-tasks programs we could use

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.