Why do integers not conform to the AnyObject protocol?

前端 未结 3 1461
一个人的身影
一个人的身影 2020-12-18 20:22

Why can I have an [AnyObject] array and put a bunch of different sized types in it ...

var a = [AnyObject]()
a.append(Int(1))
a.append(Float64(3         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 20:33

    Because Int is a value Type.

    AnyObject refers to referance types (classes) not value types

    Any refers to referance types + value types (structs + classes)

    You must use Any for valueTypes, not AnyObject

    Value Types > generally structs, enums, bla bla..

    Referance Types > generally classes

    There are two types of variables > Referance types and value types

    Referance types are pointer types and generally contains a ram address value where the original data is stored

    Value types are not addresses . They directly contain the variable value

    Int asd = 3 (asd directly contains 3)

    Int, Float, number types,... Date are almost always struct(value type) in most computer languages.

    String is almost always a hybrid in most computer languages

    ViewControllers, Views, Buttons, ...arrays, dictionaries, ... and your custom defined classes are referance types

提交回复
热议问题