Under what circumstances might `as?` produce nil where `as!` would not?

I have a giant JSON response I've deserialized into a dictionary (var json).

In lldb, I can po json as? [String: Any] and I get the expected output. When try as? [String: JSONValue]—where JSONValue is defined as below—I get nil. But when I force a downcast with as! [String: JSONValue], I don't get nil, I get the same thing as casting it to [String: Any].

Shouldn't as? and as! always produce the same case?

protocol JSONValue { }

extension String : JSONValue { }
extension NSString : JSONValue { }
extension Int : JSONValue { }
extension Bool : JSONValue { }
extension Double : JSONValue { }
extension Float : JSONValue { }
... //etc
(lldb) po json as? [String: JSONValue]
nil
(lldb) po json as! [String: JSONValue]
▿ 8 elements
  ▿ 0 : 2 elements
    - key : "foo"
    ...
  ...
1 Like

Yes. Please file a bug if you have a chance.

1 Like

Will do, thank you!

3 Likes
Terms of Service

Privacy Policy

Cookie Policy