0

From what I've understood

  • Any can represent instance of any type
  • AnyObject only represent instance from any class type

So I thought that any instance of AnyObject can also be cast as Any. Until I found this issue:

var data: NSDictionary = NSJSONSerialization.JSONObjectWithData(...)
if let dictionary = data as? Dictionary<String, Any> {
  #this code is never called

} else if let dictionary = data as? Dictionary<String, AnyObject> {
  #Surprisingly This block is called
}

How come the first block is never called whereas the second works. I know that AnyObject doesn't extend Any but I was pretty sure that i could cast any instance of AnyObject as Any.

Does someone have any clue on the matter or point me where I am wrong? Thanks

0 Answers0