markhorrocks markhorrocks - 26 days ago 30
Swift Question

Swift3 - '[String : AnyObject]' is not convertible to '[HTTPCookiePropertyKey : Any]'

I'm just new to Swift and have taken over a project written in swift 2.2 with xCode 7.4. I am converting it to xCode 8 and swift3 and have only a single compiler error left. Please help to fix this.

'[String : AnyObject]' is not convertible to '[HTTPCookiePropertyKey : Any]'


Here is the offending line of code.

let cookie = HTTPCookie(properties: dict as! [String : AnyObject] as! [HTTPCookiePropertyKey : Any])


Would this fix it?

let cookie = HTTPCookie(properties: dict as! [HTTPCookiePropertyKey : Any])

Answer

AnyObject has been replaced with Any in Swift 3 for unspecified dictionary values.

Since HTTPCookiePropertyKey is a type alias of (NS)String you can cast the type directly

let cookie = HTTPCookie(properties: dict as! [HTTPCookiePropertyKey : Any])

Maybe even

let cookie = HTTPCookie(properties: dict)

could work.

Comments