我正在使用以下代码段实现搜索。
-(void)getData:(NSString *)searchString
{
//Search string is the string the user keys in as keywords for the search. In this case I am testing with the keywords "epic headline"
searchString = [searchString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *resourcePath = [NSString stringWithFormat:@"/sm/search?limit=100&term=%@&types[]=users&types[]=questions&types[]=topics",searchString];
[self sendRequests:resourcePath];
}
//URL sent to server as a result
send Request /sm/search?limit=100&term=epic headline&types[]=users&types[]=questions&types[]=topics
我的搜索无法正常工作,因为它无法处理“史诗”和“标题”之间的空格。如何修改搜索词以便可以处理间距?
答案 0 :(得分:3)
在结果字符串上调用stringByAddingPercentEscapesUsingEncoding,根据URL规则对空格字符进行编码。