What's the difference between a protocol extended from AnyObject and a class-only protocol?

前端 未结 6 1944
一整个雨季
一整个雨季 2020-12-02 14:30

Both this declaration

protocol SomeProtocol : AnyObject {
}

and this declaration

protocol SomeProtocol : class {
}
<         


        
6条回答
  •  甜味超标
    2020-12-02 14:55

    If you open the help (alt-click) in Xcode 9 for class in a line such as protocol P: class {}, you will get typealias AnyObject.

    Thus, the code compiled (in Swift 4) will be the same whether you constrain the protocol to class or AnyObject.

    That said, there is also the question of style and future options — a future Swift version might want to treat class and AnyObject differently in some subtle way, even if that is not the case right now.

提交回复
热议问题