这是我的代码

    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是零,

展开Optional值时意外发现nil

错误来了

最佳答案

试试这个代码。这可能对您有帮助。
发生这种情况是由于NSHTTPCookie(properties: cookieProperties)返回的nil为newCookie,而您试图在nil中设置cookieStorage

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

关于ios - NSHTTPCookie无,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35474440/

10-11 14:51