2016-08-17 1 views
0

: https://somesite.com/search/name?name=%SEARCH_KEYWORD%목표 - C - 서식 GET 요청</p> <p>내 요청 URL을 같이하도록되어 GET 요청 URL에 같은 %로 존 %를 문자열 매개 변수를 전달하는 규칙은 무엇

이 시도 # 1 : 전무

# 2 시도해보십시오 :이

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://somesite.com/search/name?name=%%%@%%",SEARCH_KEYWORD]]; 

는 O/P했다

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://somesite.com/search/name?name=%JOE%"]]; 

** O/P :

https://somesite.com/search/name?name=JOE 

어떤 제안?

답변

2

당신은 URL을 구축 할 NSURLComponents를 사용할 수 있습니다

let components = NSURLComponents(string: "https://google.com")! 
components = "s=%search keywords%" 

let url = components! 
print(url) // "https://google.com?s=%25search%20keywords%25" 

:

NSURLComponents *components = [NSURLComponents componentsWithString:@"https://google.com"]; 
components.query = @"s=%search keywords%" 

NSURL *url = components.URL; 

스위프트 (생산 !주의, 나는 운동장에서 테스트하는 데 사용) : 오브젝티브 C는

또한 더 복잡한 쿼리가 필요한 경우 NSURLComponentqueryItems 속성을가집니다.

+0

감사합니다. –

-3

실수로 실수를하고 있습니다. 단 하나의 %@ 만 사용하십시오. 예 :

NSString *string = @"JOE"; 
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"https://somesite.com/search/name?name=%@",string]];