문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top