我有URL在AppDelegate方法。如何將url.query轉換爲swift中的字典?
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
}
網址看起來www.wesite.com/shareplace.html?placeid=123
我的問題是如何將它轉換成字典方便地訪問。
編輯:我發現一些網站上的一些代碼,但它顯示在Xcode 9
extension URL {
var queryDictionary: [String: AnyObject]? {
return URLComponents(url: self, resolvingAgainstBaseURL: false)?
.queryItems?
.reduce([:], combine: { (var result: [String: AnyObject], queryItem) -> [String: AnyObject] in
if queryItem.value?.containsString(",") ?? false {
let array = queryItem.value?.componentsSeparatedByString(",")
result[queryItem.name] = array
}
else {
result[queryItem.name] = queryItem.value
}
return result
})
}
}
.reduce([:], combine: { (var result: [String: AnyObject], queryItem) -> [String: AnyObject] in
(VAR結果)參數可能沒有足夠的 '變種' 說明