2010-12-03 9 views
0

일부 URL을 사용하는 iPhone 앱에서 작업 중이며 끝에 int를 추가하는 데 어려움이 있습니다. 나는 다음 코드 줄을 가지고있다.NSString으로 int 캐스팅하는 문제

 NSURL *urlCards = [[NSURL alloc] initWithString:(@"http://website.edu/get_stuff/%@",[NSString stringWithFormat:@"%d",_stuffID])]; 

나는 단지 int의 끝에 추가 할 필요가있다. NSURL urlCards의 결과를 출력 할 때, 단순히 전달중인 int 값 또는 _deckID 값을 얻습니다.

로컬 int로 선언 된 _deckID가 실제로 런타임에 올바른 값을 가지고 있음을 확인했습니다.

무엇이 누락 되었습니까?

감사합니다. u는이 같은 시도

답변

1

쉼표 연산자가 있습니다. 쉼표 연산자는 부작용에 대해 각 피연산자를 평가하고 마지막 표현식의 결과로 평가합니다. 예를 들어 :

int i; 
int j; 
int z; 

z = (i = 4, j = 3, i + j); 
// z is now 7 

당신이 여기있어 무엇 :

(@"http://website.edu/get_stuff/%@",[NSString stringWithFormat:@"%d",_stuffID]) 

는 평가 단지

[NSString stringWithFormat:@"%d", _stuffID] 

첫 번째 부분 @"..."은 부작용이없는 표현이기 때문이다, 쉼표 연산자의 결과는 [NSString stringWithFormat:] 메소드의 결과입니다.

당신은이, 내가 생각 찾고있는 무엇 :

NSString *urlString = [NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID]; 
NSURL *urlCards = [[NSURL alloc] initWithString:urlString]; 

또한 KingofBliss의 대답처럼, 한 줄에 그것을 할 수 있습니다.

0

Y의 캔트,

NSURL *urlCards = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID]]; 
0
NSString *urlString=[NSString stringWithFormat:@"http://website.edu/get_stuff/%d",_stuffID]; 
NSURL *urlCards = [[NSURL alloc] initWithString:urlString]; 

사용이이 문제를 해결합니다.

관련 문제