jeudi 5 mars 2015

Compare NSString in switch satement

I have a if statement comparing NSString like this :



NSString *str = @"whatevstring";

NSArray *array = @[@"string1",@"string2",@"string3"];

if ([str isEqualToString:@"string1"] ){
//str == string1
} else if ([str isEqualToString:@"string2"]){
//str == string2
} else if ([str isEqualToString:@"string3"]){
//str == string3
}


and I would like it alternates with switch statement,


this is how I do it:



int compareInt;

for (int i = 0; i < 4; i++) {
if ([segue.identifier isEqualToString:[array objectAtIndex:i]]) {
compareInt = i;
break;
//well, I supposed not to use any if statement...
}
}

switch (compareInt) {
case 0:
//str == string1;
break;
case 1:
//str == string2;
break;
case 2:
//str == string3;
break;
default:
break;
}


is there a better way to do this with switch statement?


or is if statement the only and best choice when it comes up with comparison in objective-C?


any suggestion welcome!


Aucun commentaire:

Enregistrer un commentaire