In Swift, what does it mean for protocol to inherit from class keyword?

后端 未结 2 487
悲&欢浪女
悲&欢浪女 2020-12-09 22:54

In Swift, what does it mean for protocol to inherit from class keyword?

e.g.

protocol MyDelegate: class {

}
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 23:40

    From the Apple docs:

    You can limit protocol adoption to class types (and not structures or enumerations) by adding the class keyword to a protocol’s inheritance list.

    Example:

    protocol AProtocol: class {   
    }
    
    //Following line will produce error: Non-class type 'aStruct' cannot conform to class protocol 'AProtocol'
    struct aStruct: AProtocol { 
    }
    

    The line declaring the structure will spit an error. Following line will produce error:

    Non-class type 'aStruct' cannot conform to class protocol 'AProtocol'

提交回复
热议问题