NSHTTPCookie无

时间:2016-02-18 06:33:01

标签: ios swift nshttpcookie

这是我的代码

    let cookieProperties = [
        NSHTTPCookieOriginURL: Constants.baseUrl,
        NSHTTPCookiePath: "/",
        NSHTTPCookieName: "device_id",
        NSHTTPCookieValue: Constants.deviceId
    ]
    let cookiePropertiesVersion = [
        NSHTTPCookieOriginURL: Constants.baseUrl,
        NSHTTPCookiePath: "/",
        NSHTTPCookieName: "app_version_code",
        NSHTTPCookieValue: "50"
    ]

    let newCookie = NSHTTPCookie(properties: cookieProperties)
    let newCookieVersion = NSHTTPCookie(properties: cookiePropertiesVersion)
    cookieStorage.setCookie(newCookie!)
    cookieStorage.setCookie(newCookieVersion!)

错误在行

  

cookieStorage.setCookie(newCookie!)

newCookie是零和

  

在解包可选值时意外发现nil

错误来了

1 个答案:

答案 0 :(得分:1)

试试这段代码。这可能对你有所帮助。 这是因为NSHTTPCookie(properties: cookieProperties)返回nil为newCookie,并且您尝试在nil中设置cookieStorage

   if let newCookie = NSHTTPCookie(properties: cookieProperties){
        cookieStorage.setCookie(newCookie!)
    }
    let newCookieVersion = NSHTTPCookie(properties: cookiePropertiesVersion)