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.
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];
}