Swift - what's the difference between metatype .Type and .self?

前端 未结 4 754
一生所求
一生所求 2020-12-02 10:52

What\'s the difference between metatype .Type and .self in Swift?

Do .self and .Type return a struct

4条回答
  •  盖世英雄少女心
    2020-12-02 11:12

    They appear in different places syntactically.

    In a place syntactically where you have to specify a type, Something.Type is a valid type, corresponding to the type that is the metatype (which is metaclass for classes) of Something. Something.self is not a valid syntax for a type.

    In a place syntactically where you have to write an expression, Something.self is a valid expression. It's an expression of type Something.Type, and the value is the thing ("class object" in the case of classes) that represents the type Something. Something.Type is not a valid expression syntax.

提交回复
热议问题