4

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;
}
Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
NoobDev4iPhone
  • 5,531
  • 10
  • 33
  • 33
  • 6
    I think you are missing basic programming knowledge... My advice, start from zero, and don't try to learn everything in a week. Also, don't use tutorials to learn, get a book or go to a class (if possible). – EmilioPelaez Mar 06 '12 at 05:11

4 Answers4

13

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;
}
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
  • 1
    why not store NSNumbers in the array? It's a bit fragile to store numbers as strings ... – bryanmac Mar 06 '12 at 04:31
  • @bryanmac That would make more sense...but NSNumber is still an object, so you'd still have to extract the integral value to use in the switch statement. – highlycaffeinated Mar 06 '12 at 04:34
  • Quick question: how do you create a Global array, so that it can be accessed from every method? – NoobDev4iPhone Mar 06 '12 at 06:10
  • "how do you create a Global array, so that it can be accessed from every method?" Thats a question that at best shows you really really need to take a step back and read a good book on objC dev and at worst will lead you to some horrible code with global values. – AlexTheMighty Mar 06 '12 at 08:16
2

You are creating an array of literal NSStrings and doing case statements on ints. You can only switch on integral types.

The problem is arrayWithObjects creates an array of NSObject derived objects which you can't switch on an object (id).

If you want to store an array of numbers, then one option is to store NSNumber objects rather than relying on the fragility of storing strings that you hope are numbers. This works:

NSArray *arr = [NSArray arrayWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];

switch ([[arr objectAtIndex:1] intValue]) {
    case 1:
        NSLog(@"1");
        break;

    case 2:
        NSLog(@"2");
        break;

    default:
        break;
}

It outputs:

2012-03-05 23:23:46.798 Craplet[82357:707] 2
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • I realized that another problem was that I initiated my Array in - (void)viewDidLoad method, but I can't access it from another method. How do you create a global array (global vars in general)? – NoobDev4iPhone Mar 06 '12 at 05:53
  • Checkout the singleton pattern. – bryanmac Mar 06 '12 at 06:32
  • http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like – bryanmac Mar 06 '12 at 06:33
  • the other thing you can do is delegates (have one view provide a delegate for the other view to call back and get data). depends whether it's views sharing data or you really want it to be a single global object ... – bryanmac Mar 06 '12 at 06:34
1

[count objectAtIndex:] returns an Id (aka object), which in your particular case would be a NSString, in either case, its not an int that your case expression is expecting. You'll want [[count objectAtIndex:myIndex] intValue] to convert the NSString to an integer.

superfell
  • 18,780
  • 4
  • 59
  • 81
1

Your array objects are NSStrings, not ints. What is the larger picture here that you're trying to accomplish?

You could:

NSString *str = [count objectAtIndex:myIndex];
if ([str isEqualToString:@"1"]) NSLog(@"One");
else if ... // etc

Better yet:

static NSString *one = @"1";
static NSString *two = @"2";
// etc

NSArray *count = [NSArray arrayWithObjects:one, two, ... nil];

NSString *str = [count objectAtIndex:myIndex];

if (str == one) NSLog(@"One"); // this works because now 'str' and 'one' are the same object
else if ... // etc
QED
  • 9,803
  • 7
  • 50
  • 87