lakshmen
lakshmen

Reputation: 29064

Converting string to an integer in ios

I know this question has been asked many times. You can convert string to integer in this two ways:

[myString intValue] which returns a "C" type or [myString integerValue] which returns NSInteger.

But my requirement looks like this:

int route1 = 12,route2 = 5,route3 = 6;
for (int j = 1; j < 4; j++) {
        int route = [[NSString stringWithFormat:@"route%i",j] integerValue];
        //do something using the route
}

I need the route variable to take the values of the different routes depending on the value of variable j.

How do i do it? Need some guidance on this.

Upvotes: 0

Views: 2996

Answers (2)

Dan
Dan

Reputation: 1

Not fully sure what you are trying to do here. You should probably use a route integer array or something and reference the indexes. From what it looks like you are trying to get the values from "route1", "route2", "route3".

You should look at the use of intValue or integerValue. If you had an NSString who's value was "1234" it would return the integer 1234.

Upvotes: 0

giorashc
giorashc

Reputation: 13713

You can do the following :

int routes[3] = {12, 5, 6};

and in your for loop :

int route = routes[j];

No need to convert anything

Upvotes: 4

Related Questions