Trying to work with array, but it gives me "Statement requires expression of integer type('id' invalid)" right at switch statement. What's wrong?
NSArray count = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
switch ([count objectAtIndex: myIndex]) {
case 1:
NSLog(@"One");
break;
case 2:
NSLog(@"Two");
break;
case 3:
NSLog(@"Three");
break;
default:
break;
}
A switch statement only works on integral types. Your array contains NSString
objects. Convert the NSString
that you get from the array to an integer like this:
NSArray count = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
NSString *obj = [count objectAtIndex: myIndex];
switch ([obj intValue]) {
case 1:
NSLog(@"One");
break;
case 2:
NSLog(@"Two");
break;
case 3:
NSLog(@"Three");
break;
default:
break;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With