stringswiftoption-typeconditional-binding

Initializer for conditional binding must have Optional type, not 'String' on HTTPCookieStorage


Not sure why I'm getting this error, but I get it when I updated to Swift 2.

The error is on if let token = cookie.value {

Initializer for conditional binding must have Optional type, not 'String'

func saveAuthToken() {
    if let cookies = VPAPICall.sharedInstance.session?.configuration.HTTPCookieStorage?.cookies {
        for cookie in cookies {
            if cookie.name.uppercaseString == "VIEQUES_SESSION_ID" {
                if let token = cookie.value {
}

Solution

  • It's because cookie.value is not optional, it's type is a String.

    You cannot use the if let syntax on non-optional types.

    It should be let token = cookie.value, or use .value directly.