How can I replace the if-else statement for something way shorter?

91 views Asked by At

I think the following code speaks for itself.

if (card==1) {
    cardImageString = @"myGrabbedImage1.png";
} else if (card==2) {
    cardImageString = @"myGrabbedImage2.png";
} else if (card==3) {
    cardImageString = @"myGrabbedImage3.png";
} else if (card==4) {
    cardImageString = @"myGrabbedImage4.png";
} else if (card==5) {
    cardImageString = @"myGrabbedImage5.png";
} else if (card==6) {
    cardImageString = @"myGrabbedImage6.png";
} else if (card==7) {
    cardImageString = @"myGrabbedImage7.png";
} else if (card==8) {
    cardImageString = @"myGrabbedImage8.png";
}

The actual if-else is 10 times bigger than this.

Card = int.

CardImageString = NSString.

'card' is only used in this statement and can be removed in your answer.

How can I simplify all this??? Thanks!!

2

There are 2 answers

4
Krys Jurgowski On BEST ANSWER
cardImageString = [NSString stringWithFormat:@"myGrabbedImage%@.png", @(card)];
0
Onik IV On
    cardImageString = [NSString stringWithFormat:@"myGrabbedImage%d.png",card];