最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】iOS的Swift中如何把JSON字典对象编码为url的query string

iOS crifan 5262浏览 0评论

折腾:

【已解决】iOS中UIWebView中如何改变URL并刷新页面

期间,想要实现,url中的query string编码

变成:

http://xx.xx.xx.xx/skrDev/src/report/dealer.html?t=1510899512424&cid=2

想起来了,貌似Alamofire中有这个功能?

swift url query string encode

ios – Swift – encode URL – Stack Overflow

ios – How to encode a URL in Swift – Stack Overflow

ios – Swift: string url encoding not working as expected – Stack Overflow

ios – encoding url using swift code – Stack Overflow

swift url json to query string

ios – How to send Json as parameter in url using swift – Stack Overflow

ios – Best way to parse URL string to get values for keys? – Stack Overflow

ios – Sending json object in GET request in swift in Alamofire – Stack Overflow

Convert dictionary to query string in swift? – Stack Overflow

Building URLs with NSURLComponents and NSURLQueryItems – Grok Swift

然后又遇到:

【已解决】swift出错:Closure tuple parameter ‘(key: String, value: Any)’ does not support destructuring with implicit parameters

【总结】

此处可以用:

//convert query dict to query string

//    {

//        “t” : 1510906527744,

//        “tabId” : “”

//    }

// -> “?t=1510906527744&tabId=”

//

func queryDictToStr(queryParaDict: [String: Any]) -> String {

    var urlComponents = URLComponents()

    urlComponents.queryItems = queryParaDict.map { (arg) -> URLQueryItem in

        let (key, value) = arg

        let valueStr = “\(value)”

        return URLQueryItem(name: key, value: valueStr)

    }

    var queryParaStr = “”

    if let componentsAbsUrl = urlComponents.url?.absoluteString {

        queryParaStr = componentsAbsUrl //”?t=1510906527744&tabId=”

    }

    

    let encodedQueryParaStr = queryParaStr.encodedUrl

    return encodedQueryParaStr

}

将dict的query parameters:

{

   “t” : 1510906527744,

   “tabId” : “”

}

转换为问号开头的查询字符串了:

?t=1510906527744&tabId=

后续优化:

【已解决】swift中如何把base的url传入URLComponents

转载请注明:在路上 » 【已解决】iOS的Swift中如何把JSON字典对象编码为url的query string

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.189 seconds, using 22.12MB memory