最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

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

iOS crifan 680浏览 0评论

折腾:

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

期间,在Xcode9.1和Swift 3.2 or 4 ?

参考:

Convert dictionary to query string in swift? – Stack Overflow

写的代码:

            var urlParaDict = [String: Any]()

//            urlParaDict[“t”] = String(Date().toTimestamp())

            urlParaDict[“t”] = Date().toTimestamp()

            urlParaDict[“tabId”] = tabId

            let urlParams = self.dictToQueryParaStr(paraDict: urlParaDict)

            self.url = newUrl + “?\(urlParams)”

            self.loadCurUrlWebview()

        })

    }

    func dictToQueryParaStr(paraDict: [String: Any]) -> String {

        var urlComponents = URLComponents()

        urlComponents.queryItems = paraDict.map {

            URLQueryItem(name: $0, value: $1)

        }

出错:

Closure tuple parameter ‘(key: String, value: Any)’ does not support destructuring with implicit parameters

Closure tuple parameter ‘(String, String)’ does not support destructuring with implicit parameters · Issue #2141 · Alamofire/Alamofire

换成:

        urlComponents.queryItems = paraDict.map { $0, $1 in

            URLQueryItem(name: $0, value: $1)

        }

还是出错:

Consecutive statements on a line must be separated by ‘;’

Cannot convert value of type ‘(key: String, value: Any)’ to closure result type ‘URLQueryItem’

结果:

        urlComponents.queryItems = paraDict.map { eachDict in

            URLQueryItem(name: eachDict.name, value: eachDict.value)

        }

出错:

Value of tuple type ‘(key: String, value: Any)’ has no member ‘name’

ios – Closure tuple does not support destructuring in Xcode9 Swift4 – Stack Overflow

Developers – Does not compile with Swift 4 –

Closure tuple parameter does not support destructuring with implicit parameters

It doesn’t work on Xcode 9.0- beta 2 with Swift 4 · Issue #2196 · Alamofire/Alamofire

[swift-evolution] Revisiting SE-0110

【总结】

最后改为:

    func dictToQueryParaStr(paraDict: [String: Any]) -> String {

        var urlComponents = URLComponents()

//        urlComponents.queryItems = paraDict.map {

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

            let (key, value) = arg

            let valueStr = “\(value)”

            return URLQueryItem(name: key, value: valueStr)

        }

即可。

转载请注明:在路上 » 【已解决】swift出错:Closure tuple parameter ‘(key: String, value: Any)’ does not support destructuring with implicit parameters

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
14 queries in 0.330 seconds, using 10.30MB memory