Right now in my weather app, I have one section of code in my View Controllers to set up the condition image and the background image. It has about 400 lines of if
-else
statement at the end.
The app's performance is fine, but is it bad to have this? Would it be something that, say, Apple would consider rejecting? The code is very easy to read, and makes perfect sense in my opinion.
- (void)updateImages:(ICB_WeatherConditions *)weather {
if ([condition isEqualToString:@"113"]) {
conditionsImageView.image = [UIImage imageNamed:@"Sun.png"];
BGView.image = [UIImage imageNamed:@"Sun.jpg"];
} else {
if ([condition isEqualToString:@"116"]) {
conditionsImageView.image = [UIImage imageNamed:@"Mostly_Sunny.png"];
BGView.image = [UIImage imageNamed:@"Partly_Cloudy.jpg"];
} else {
if ([condition isEqualToString:@"119"]) {
conditionsImageView.image = [UIImage imageNamed:@"Overcast.png"];
BGView.image = [UIImage imageNamed:@"Overcast.jpg"];
}
else {
if ([condition isEqualToString:@"122"]) {
conditionsImageView.image = [UIImage imageNamed:@"Overcast.png"];
BGView.image = [UIImage imageNamed:@"Overcast.jpg"];
}
else {
if ([condition isEqualToString:@"143"]) {
conditionsImageView.image = [UIImage imageNamed:@"Mist.png"];
BGView.image = [UIImage imageNamed:@"Foggy.jpg"];
}
else {
if ([condition isEqualToString:@"176"]) {
conditionsImageView.image = [UIImage imageNamed:@"Scattered_Thunderstorms.png"];
BGView.image = [UIImage imageNamed:@"Scat_Tstorms.jpg"];
}
It keeps going on after that. As you can see, I have a lot of nearly identical statements, which is because I have a lot of conditions. Is it bad practice to do this? If it is, then how can this become more efficient?
NSString
. But your code would be more readable if you write your conditions like this: Can Objective-C switch on NSString? \$\endgroup\$[NSString integerValue]
for a switch statement. \$\endgroup\$