Why do integers not conform to the AnyObject protocol?

前端 未结 3 1336
一个人的身影
一个人的身影 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

    0 讨论(0)
  • 2020-12-18 20:35

    The protocol that gives Int et al this ability is _ObjectiveCBridgeable, which you can adopt yourself if you feel adventurous.

    0 讨论(0)
  • 2020-12-18 20:48

    There are two kinds of anything types in Swift – Any, which can truly hold anything – a struct, enum or class, and AnyObject, which can only hold classes.

    The reason why it seems like AnyObject can hold structs sometimes is that some specific types are implicitly converted to their NSEquivalent for you as needed, to make Objective-C interop less painful.

    When you write let ao: AnyObject = Int(1), it isn’t really putting an Int into an AnyObject. Instead, it’s implicitly converting your Int into an NSNumber, which is a class, and then putting that in.

    But only some types have this implicit conversion. Int does, but Int32 doesn’t, hence this behaviour:

    // fine
    let aoInt: AnyObject = Int(1) as NSNumber
    // also fine: implicit conversion
    let aoImplicitInt: AnyObject = Int(1)
    // not fine: error: 'Int32' is not convertible to 'NSNumber'
    let aoInt32: AnyObject = Int32(1) as NSNumber
    // but the implicit error is less, ahem, explicit
    // error: type 'Int32' does not conform to protocol 'AnyObject'
    let aoImplicitInt32: AnyObject = Int32(1)
    

    It could be argued there should be more implicit conversions to grease the wheels, but then again, these implicit conversions are the source of much confusion already and the direction in the latest beta is to have fewer of them rather than more.

    0 讨论(0)
提交回复
热议问题