为什么指定值为零?

时间:2017-02-20 04:19:26

标签: ios string null swift3

我使用oAuth登录用户。

func getUserInfoResponse(_ response: APIResponse!) {
    print("----------------------------------------")
    print("用户资料获取成功:")
    print(response.jsonResponse)
    //print(response.jsonResponse["figureurl_qq_2"])

    let accessToken = _tencentOAuth.accessToken
    let nickname    = response.jsonResponse["nickname"] as! String!
    let avatar      = response.jsonResponse["figureurl_qq_1"] as! String!
    let avatar2     = response.jsonResponse["figureurl_qq_2"] as! String!
    let urlString:String = "http://www.xxxxxx.com/cloud/app?openid=\(accessToken!)&nickname=\(nickname!)&avatar=\(avatar!)&avatar2=\(avatar2!)"
    self.wk = WKWebView(frame: self.view.frame)
    let url = URL(string: urlString)!
    self.wk.load(URLRequest(url: url))
    self.view.addSubview(self.wk)

}

print(urlString)

  

http://www.example.com/cloud/app?openid=xxxxxxx32D1E05D95E91881A15A8CDC75&nickname=   &安培;化身= http://q.qlogo.cn/qqapp/101237639/90012AB5E745A1B10A6F5F4A14F0B48D/40&avatar2=http://q.qlogo.cn/qqapp/101237639/90012AB5E745A1B10A6F5F4A14F0B48D/100

当用户登录时,所有值都具有除url之外的值并得到此错误:

  

致命错误:在解包可选值时意外发现nil

enter image description here

enter image description here

为什么url为nil,已为其赋值。对不起,我是ios和swift3的新手。

1 个答案:

答案 0 :(得分:1)

您应该在使用之前对网址进行编码

if let urlEncoded  = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
    let url = URL(string: urlEncoded)
}