According to Apple’s Swift documentation,

  • Any can represent an instance of any type at all, including function types and optional types.
  • AnyObject can represent an instance of any class type.

So in theory, AnyObject shouldn’t work with Struct, Int,… (value types). But it did. You have to cast them to AnyObject, but how we can cast a value type to an Object. Oh, that did make me utterly confused.
Now, time for Object-C, remember that NSString and NSNumber are reference types in Object-C. also, Apple said:
“Swift automatically converts some Objective-C types to Swift types, and some Swift types to Objective-C types. Types that can be converted between Objective-C and Swift are referred to as bridged types.”
Ok, that makes sense! What really going on under the hood is: Int and String was converted to NSNumber and NSString before be casted to AnyObject

A nature, universe, science, music, love lover

Leave a Reply

Your email address will not be published. Required fields are marked *