哪种结构更适合这种情况,if-else 还是 switch?
- (id)hexEvaluator:(double)remainder
{
if(remainder == 0) {return [NSNumber numberWithInt:0];}
else if(remainder == 1) {return [NSNumber numberWithInt:1];}
else if(remainder == 2) {return [NSNumber numberWithInt:2];}
else if(remainder == 3) {return [NSNumber numberWithInt:3];}
else if(remainder == 4) {return [NSNumber numberWithInt:4];}
else if(remainder == 5) {return [NSNumber numberWithInt:5];}
else if(remainder == 6) {return [NSNumber numberWithInt:6];}
else if(remainder == 7) {return [NSNumber numberWithInt:7];}
else if(remainder == 8) {return [NSNumber numberWithInt:8];}
else if(remainder == 9) {return [NSNumber numberWithInt:9];}
else if(remainder == 10) {return @"A";}
else if(remainder == 11) {return @"B";}
else if(remainder == 12) {return @"C";}
else if(remainder == 13) {return @"D";}
else if(remainder == 14) {return @"E";}
else if(remainder == 15) {return @"F";}
else return nil;
switch ((int)remainder)
{
case 0: return [NSNumber numberWithInt:0]; break;
case 1: return [NSNumber numberWithInt:1]; break;
case 2: return [NSNumber numberWithInt:2]; break;
case 3: return [NSNumber numberWithInt:3]; break;
case 4: return [NSNumber numberWithInt:4]; break;
case 5: return [NSNumber numberWithInt:5]; break;
case 6: return [NSNumber numberWithInt:6]; break;
case 7: return [NSNumber numberWithInt:7]; break;
case 8: return [NSNumber numberWithInt:8]; break;
case 9: return [NSNumber numberWithInt:9]; break;
case 10: return @"A";; break;
case 11: return @"B";; break;
case 12: return @"C";; break;
case 13: return @"D";; break;
case 14: return @"E";; break;
case 15: return @"F";; break;
default: return nil; break;
}
}
另外,附带说明:为什么不允许开关表达式(在我的情况下为余数)为“双”类型?我必须将其转换为“int”才能编译。