从AnyObject扩展的协议和仅类协议有什么区别? [英] What's the difference between a protocol extended from AnyObject and a class-only protocol?
问题描述
这个声明
protocol SomeProtocol:AnyObject {
}
和此声明
protocol SomeProtocol:class {
}
似乎使得只有类可以符合这个协议该协议的实例是对对象的引用),并没有其他影响。
它们之间有什么区别吗?应该优先于另一个?如果没有,为什么有两种方法做同样的事情?
我使用最新发布的Xcode 6.3.1。
更新:在咨询之后, / a>,这两个定义是假设是等价的, AnyObject
用作替代,
正在完成。
不同之处在于的语义, @objc
声明。使用 AnyObject
,期望是符合类可能或不是适当的Objective-C对象,但是语言对待它们,因为你有时会失去静态分派, 。这个的好处是,你可以处理 AnyObject
等。协议约束作为一种询问 @objc
成员函数的方法,如STL中 AnyObject
文档中的示例所示:
import Foundation
class C {
@objc func getCValue() - > int {return 42}
}
//如果x有一个方法@objc getValue() - > Int,调用它,
//返回结果。否则,返回nil。
func getCValue1(x:AnyObject) - > Int? {
if let f:() - > Int = x.getCValue {//< ===
return f()
}
return nil
}
//使用可选链接的更为惯用的实现
func getCValue2(x:AnyObject) - > Int? {
return x.getCValue?()//< ===
}
//假定存在所需方法的实现
func getCValue3(x:AnyObject) - > Int {//< ===
return x.getCValue()// x.getCValue被隐式解包。 //< ===
}
到 class
-deriving协议:
import Foundation
/ pre>
protocol SomeClass:class {}
class C:SomeClass {
@objc func getCValue() - > int {return 42}
}
//如果x有一个方法@objc getValue() - > Int,调用它,
//返回结果。否则,返回nil。
func getCValue1(x:SomeClass) - > Int? {
if let f:() - > Int = x.getCValue {//< === SomeClass没有成员getCValue
return f()
}
return nil
}
//使用可选链接的更为惯用的实现
func getCValue2(x:SomeClass) - > Int? {
return x.getCValue?()//< === SomeClass没有成员getCValue
}
//一个实现,
func getCValue3(x:SomeClass) - > int {//< ===
return x.getCValue()//< === SomeClass没有成员'getCValue'
}
所以看起来
class
是一个更保守的版本AnyObject
,当你只关心引用语义,而不是关于动态成员查找或Objective-C桥接时,应该使用它。Both this declaration
protocol SomeProtocol : AnyObject { }
and this declaration
protocol SomeProtocol : class { }
seem to make it so that only classes can conform to this protocol (i.e. that the instances of the protocol are references to objects), and have no other effects.
Is there any difference between them? Should one be preferred over the other? If not, why is there two ways to do the same thing?
I am using the latest released Xcode 6.3.1.
解决方案Update: After consulting with the powers that be, the two definitions are supposed to be equivalent, with
AnyObject
being used as a stand-in whileclass
was being finished. In the future the latter will obviate the former but, for now, they do present a few minor differences.The difference lies in the semantics of
@objc
declarations. WithAnyObject
, the expectation is that conforming classes may or may not be proper Objective-C objects, but the language treats them as such anyway (in that you lose static dispatch sometimes). The takeaway from this is that you can treat anAnyObject
et al. protocol constraint as a way to ask for@objc
member functions as shown in the example in documentation forAnyObject
in the STL:import Foundation class C { @objc func getCValue() -> Int { return 42 } } // If x has a method @objc getValue()->Int, call it and // return the result. Otherwise, return nil. func getCValue1(x: AnyObject) -> Int? { if let f: ()->Int = x.getCValue { // <=== return f() } return nil } // A more idiomatic implementation using "optional chaining" func getCValue2(x: AnyObject) -> Int? { return x.getCValue?() // <=== } // An implementation that assumes the required method is present func getCValue3(x: AnyObject) -> Int { // <=== return x.getCValue() // x.getCValue is implicitly unwrapped. // <=== }
The same example falls over immediately if you change that to a
class
-deriving protocol:import Foundation protocol SomeClass : class {} class C : SomeClass { @objc func getCValue() -> Int { return 42 } } // If x has a method @objc getValue()->Int, call it and // return the result. Otherwise, return nil. func getCValue1(x: SomeClass) -> Int? { if let f: ()->Int = x.getCValue { // <=== SomeClass has no member 'getCValue' return f() } return nil } // A more idiomatic implementation using "optional chaining" func getCValue2(x: SomeClass) -> Int? { return x.getCValue?() // <=== SomeClass has no member 'getCValue' } // An implementation that assumes the required method is present func getCValue3(x: SomeClass) -> Int { // <=== return x.getCValue() // <=== SomeClass has no member 'getCValue' }
So it seems
class
is a more conservative version ofAnyObject
that should be used when you only care about reference semantics and not about dynamic member lookups or Objective-C bridging.这篇关于从AnyObject扩展的协议和仅类协议有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!