try? JSONSerialization.jsonObject(with: data) as? [String:Any]
interpreted as
try? (JSONSerialization.jsonObject(with: data) as? [String:Any])
what makes it "double optional" like [String:Any]?? . Optional binding removes only one level, so json is of type [String:Any]?
The problem is solved by setting parentheses:
guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String:Any] else { break }
And just for fun: Another (less obvious ?, obfuscation?) Solution is to use a matching pattern with a double optional pattern:
guard case let json?? = try? JSONSerialization.jsonObject(with: data) as? [String:Any] else { break }
source share