hanumanDev
hanumanDev

Reputation: 6614

How do I change this if conditional into a switch statement?

I tried to change the following code from an if statement into a switch statement but I get an error saying that I need an integer and not an id object.

here's my switch:

-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];

switch ([colorNames objectAtIndex:])) {
    case 1:
        [redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];

        break;

    case 2:
        [redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];

        break;

    default:
        break;
}

and here's my if conditional statement:

-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];


if ([colorNames objectAtIndex:0]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:1]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:2]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_3.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:3]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_4.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:4]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_5.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:5]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_6.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:6]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_7.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:7]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_8.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:8]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_9.png"] forState:UIControlStateNormal];
}

}

thanks for any help

Upvotes: 0

Views: 238

Answers (6)

Mario
Mario

Reputation: 4520

Okay, I couldnt help but answer, although most has already been said.

This is your original if-statement you want to change to switch statement:

-(void) nearestIndexAfterRotationEnded:(int)index {
NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];


if ([colorNames objectAtIndex:0]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_1.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:1]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_2.png"] forState:UIControlStateNormal];

}

if ([colorNames objectAtIndex:2]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_3.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:3]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_4.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:4]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_5.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:5]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_6.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:6]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_7.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:7]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_8.png"] forState:UIControlStateNormal];
}

if ([colorNames objectAtIndex:8]) {
    [redButton setImage:[UIImage imageNamed:@"Btn1_9.png"] forState:UIControlStateNormal];
}
}

Now this is not possible, since you need a variable to switch.

Now, all the answers dont see, that what you are doing in the if statements has nothing to do with the passed index variable. It never appears in there. (which I think is by mistake)

Instead, you check from 0-8 if an object exists at index 0-8 in your colorNames array. If any of the if statements fail, it means that all the following will fail, too because

if ([colorNames objectAtIndex: i]) 

returns NIL, it means that this is the end of the array!

So checking if ([colorNames objectAtIndex: 0-8]) is essentially pointless or at least calls for simplification. This whole if-statement thing can be condensed to one line of code:

        [redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", colorNames.count]] forState:UIControlStateNormal];

That is, if the colorNames doesnt hold MORE than 9 objects. If it holds more than 9 objects and you still want to check only if the first 9 exist, this one line of code does the trick:

[redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", MAX(9,colorNames.count)]] forState:UIControlStateNormal]; 

Remember, in the whole if-thing the passed in index variable isnt taken into account.

If that was by mistake and you want to return the name of the object in your array at index, also no multiple ifs would be needed:

if ([colorNames objectAtIndex: index])
 [redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", index+1]] forState:UIControlStateNormal];

So I think, you either need to clarify your question (because more cannot be done with your code snippet) or improve your code.

Upvotes: 1

NeverBe
NeverBe

Reputation: 5037

- (void)nearestIndexAfterRotationEnded:(int)index {
    NSLog(@"Selected item is: %@", [colorNames objectAtIndex:index]);
    statusLabel.text = [NSString stringWithFormat:@"Selected item is: %@", [colorNames objectAtIndex:index]];

    for (int i=0; i<9; i++) {
        if ([colorNames objectAtIndex:i]) {
            [redButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Btn1_%d.png", i+1]] forState:UIControlStateNormal];
        }
    }
}

Upvotes: 5

Hariprasad.J
Hariprasad.J

Reputation: 307

Change it the code 
 switch ([colorNames objectAtIndex:]))
to 
switch (index)  This will be helpful for you.

Upvotes: 1

The dude
The dude

Reputation: 7924

You have to use int in a switch. So, you have to convert [colorNames objectAtIndex:1] to an integer. I don't know what are the objects at color, whether they are an NSNumber or NSString, but I am going to assume the latest.

You have to switch like this:

switch ([[colorNames objectAtIndex:index] intValue]))

The intValue message tells the NSString to return an integer representation of it. That way you can switch.

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

You cannot change that into a switch statement as it does not fulfil the prerequisites for a switch.

Switch always follows the pattern

switch aVar {

case value1:
case value2:
default: 

}

You would have to find an alternative algorithm if you insist on using a switch statement.

Upvotes: 1

user1567896
user1567896

Reputation: 2398

Can you run this code?

switch ([colorNames objectAtIndex:])) {

should throw an error. You are not passing a value to objectAtIndex!

It should be

switch ([colorNames objectAtIndex:index])) {

Upvotes: -2

Related Questions