EDIT: One of the bigger questions I have is that, is using switch-case more efficient then using this?
Right now in my application (a weather app), I have one section of code in my View Controllers to set up the condition image, and the background image. It's about 400 lines of if-else statement at the end. The app's performance is fine and works fast, but is it bad to have this? Would it be something that say Apple would be looking at and would reject? The code is very easy to read, and makes perfect sense in my opinion. Here's a snippet:
NOTE THIS IS BAD CODE, I DID NOT USE ELSE-IF's AND HAD NESTED STATEMENTS, AND IT IS NOT EASILY READABLE, AND HAS DUPLICATED CODE
- (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, the statements are almost identical, and the only reason I have a lot is because I have a lot of conditions. Is it bad practice to do this? and if it is, is there any way that is more efficient then this? Thanks!
NSString. But your code would be more readable if you write your conditions like this: Can Objective-C switch on NSString? – Florent Jul 12 '12 at 14:15[NSString integerValue]for a switch statement. – Christoph Winkler Jul 12 '12 at 14:18