这是我的代码
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
错误来了
答案 0 :(得分:1)
试试这段代码。这可能对你有所帮助。
这是因为NSHTTPCookie(properties: cookieProperties)
返回nil为newCookie
,并且您尝试在nil
中设置cookieStorage
。
if let newCookie = NSHTTPCookie(properties: cookieProperties){
cookieStorage.setCookie(newCookie!)
}
let newCookieVersion = NSHTTPCookie(properties: cookiePropertiesVersion)