Distinction in Swift between uppercase “Self” and lowercase “self”

前端 未结 6 1024
心在旅途
心在旅途 2020-12-07 10:53

While playing in a Swift playground I noticed that Self, with capital \"S\", is available along with the lowercase self. Is there any difference be

6条回答
  •  遥遥无期
    2020-12-07 11:22

    Self can also be used as a return type in the protocol extension method body which will return confirming type instance, and for type casting with "as". Please see example below:

    extension  where Self: {
    static func foo(_ param:Type)-> Self{
        guard let abc = method() as? Self else{
            return xyz
        }
    }}
    

    In the nutshell, Self can be used to refer the Type which confirms to the protocol.

提交回复
热议问题