[英]iOS Swift how to use a string containing %2 in a NSURL?
I am very new to to using web services and coding in general. 我对使用Web服务和一般编码非常陌生。 So please let me know if i am not making any sense.
因此,如果我没有任何道理,请告诉我。
I am using two methods to construct an url to use to call the flickr API. 我正在使用两种方法来构造用于调用flickr API的URL。 Everything works well, the only thing i can't make work is using the "%2Curl_m" parameter in my url.
一切正常,我唯一无法做的就是在URL中使用“%2Curl_m”参数。 When the url gets constructed "%2Curl_m" becomes "%252Curl_m".
构造URL后,“%2Curl_m”将变为“%252Curl_m”。
I've tried google and searching here but i didn't find an answer. 我已经尝试过Google并在这里搜索,但没有找到答案。 The only thing that came close is that it might have to do with string encoding.
唯一接近的是它可能与字符串编码有关。 I've looked in the documentation but i really couldn't make much sense out of it.
我已经看过文档,但是我真的没有多大意义。
I am using this two methods to construct the url: 我正在使用这两种方法来构造url:
private class func flickrURL(method method: Method, page:Int,
parameters: [String:String]?) -> NSURL {
let components = NSURLComponents(string: baseURLString)!
var queryItems = [NSURLQueryItem]()
let baseParams = [
"method": method.rawValue,
"format": "json",
"nojsoncallback": "1",
"api_key": APIKey,
"text": "car",
"per_page": "50",
"page": String(page)
]
for (key, value) in baseParams {
let item = NSURLQueryItem(name: key, value: value)
queryItems.append(item)
}
if let additionalParams = parameters {
for (key, value) in additionalParams {
let item = NSURLQueryItem(name: key, value: value)
queryItems.append(item)
}
}
components.queryItems = queryItems
print(components.URL!)
return components.URL!
}
private static var nextPage = 0
class func recentPhotosURL() -> NSURL {
nextPage += 1
print(nextPage)
let curl = "%2Curl_m"
print(curl)
return flickrURL(method: .RecentPhotos, page: nextPage,
parameters: ["extras": "\(curl),url_h,date_taken"])
}
The url I get is: 我得到的网址是:
https://api.flickr.com/services/rest?page=2&text=car&api_key=XXXXXXXXXXXXXXXXXXX&method=flickr.photos.search&per_page=50&format=json&nojsoncallback=1&extras=%252Curl_m,url_h,date_taken
The url I need is: 我需要的网址是:
https://api.flickr.com/services/rest?page=2&text=car&api_key=XXXXXXXXXXXXXXXXXXX&method=flickr.photos.search&per_page=50&format=json&nojsoncallback=1&extras=%2Curl_m,url_h,date_taken
These 3 lines: 这3行:
let curl = "%2Curl_m"
print(curl)
return flickrURL(method: .RecentPhotos, page: nextPage, parameters: ["extras": "\(curl),url_h,date_taken"])
A quick look at the Flickr API tells me that it's not a valid value in the extras
parameter, that would be url_m
. 快速浏览Flickr API会告诉我,这不是
extras
参数中的有效值,它是url_m
。
%2C
is the percent-encoded form for comma ( ,
). %2C
是逗号( ,
)的百分比编码形式。 NSURLQueryItem
thinks you mean the string %2C
so it encodes the %
sign to become %25
. NSURLQueryItem
认为您的意思是字符串%2C
因此它将%
符号编码为%25
。 That's how you get %252Curl_m
. 这就是您获取
%252Curl_m
。
I don't know where you picked up the %2C
. 我不知道您在哪里拿了
%2C
。 Did use a web utility and included the comma by mistake? 是否使用了网络工具并错误地包含了逗号?
When your URL is translated into a String, some characters are escaped like %
translates to %25
and so… 将您的URL转换为字符串时,某些字符会被转义,例如
%
转换为%25
,因此…
You can get the non-escaped string with stringByRemovingPercentEncoding
. 您可以使用
stringByRemovingPercentEncoding
获得未转义的字符串。
print("%252".stringByRemovingPercentEncoding!) // prints "%2"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.