Swift 2.0:协议扩展:具有相同功能签名的两个协议编译错误
给出两个协议及其扩展名:
Given the two protocols and their extensions:
protocol FirstDelegate {
func someFunc()
}
protocol SecondDelegate {
func someFunc()
}
extension FirstDelegate {
func someFunc() {
print("First delegate")
}
}
extension SecondDelegate {
func someFunc() {
print("Second delegate")
}
}
并尝试同时遵守它们:
class SomeClass: FirstDelegate, SecondDelegate {}
我收到编译时错误:
类型"SomeClass"不符合协议"FirstDelegate"
Type 'SomeClass' does not conform to protocol 'FirstDelegate'
交换FirstDelegate
和SecondDelegate
:
class SomeClass: SecondDelegate, FirstDelegate {}
产生反向:
类型"SomeClass"不符合协议"SecondDelegate"
Type 'SomeClass' does not conform to protocol 'SecondDelegate'
删除扩展名之一即可解决此问题.同上为SomeClass
中的someFunc()
提供实现.
Removing one of the extensions resolves the problem. Ditto providing implementation for someFunc()
inside SomeClass
.
这个协议扩展功能对我来说还很新.另外,目前苹果官方的《快速编程指南(预发行版)》中有关此信息的信息还很少.
This protocol extension functionality is rather new to me. Also the information about it in an Apple's official 'Swift Programming Guide (Prerelease)' is scarce at the moment.
我在这里违反了某些协议扩展规则吗?
Did I violate some rules of protocol extensions here?
协议定义了对 一致的类型.
A protocol defines requirements (methods, properties, ...) for a conformant type.
protocol FirstDelegate {
func someFunc()
}
protocol SecondDelegate {
func someFunc()
}
使用相同的必需方法someFunc()
定义两个协议.
一致类型必须实现此方法:
defines two protocols with the same required method someFunc()
.
A conformant type must implement this method:
class SomeClass: FirstDelegate, SecondDelegate {
func someFunc() {
print("SomeClass implementation")
}
}
协议扩展提供了方法和属性的实现 符合类型.协议扩展的特殊情况是 默认实现,即您在此处定义的内容:
A protocol extension provides method and property implementations to conformant types. A special case of a protocol extension is a default implementation, which is what you defined here:
extension FirstDelegate {
func someFunc() {
print("First delegate")
}
}
它为所有类型定义someFunc()
的默认实现
符合FirstDelegate
.由于这是 only 必需的
该协议的方法,一致的类无需定义
方法:
It defines a default implementation of someFunc()
for all types
conforming to FirstDelegate
. Since this is the only required
method of that protocol, a conforming class need not define the
method at all:
class SomeClass: FirstDelegate {
}
SomeClass().someFunc() // Output: First delegate
但是 if 该类提供了自己的实现, 将被使用:
But if the class provides its own implementation then that will be used:
class SomeClass: FirstDelegate {
func someFunc() {
print("SomeClass implementation")
}
}
SomeClass().someFunc() // Output: SomeClass implementation
在您的情况下,您已定义someFunc()
的默认实现
对于两种协议:
In your case, you have defined default implementations of someFunc()
for both protocols:
extension FirstDelegate {
func someFunc() {
print("First delegate")
}
}
extension SecondDelegate {
func someFunc() {
print("Second delegate")
}
}
如果一个类提供了自己的类,则它仍然可以同时符合这两种协议 所需方法的实现:
A class can still conform to both protocols if it provides its own implementation of the required method:
class SomeClass: FirstDelegate, SecondDelegate {
func someFunc() {
print("SomeClass implementation")
}
}
但是类 不能通过使用默认实现来实现
But the class cannot conform by using the default implementation
class SomeClass: FirstDelegate, SecondDelegate {
}
两种协议的
因为发生了冲突.未指定哪个默认值 应该使用实现,这就是编译器抱怨的原因.
for both protocols because there is a conflict. It is unspecified which default implementation should be used, and that's why the compiler complains.
实际上,该类现在符合协议的 none . 在报告导航器的完整编译器日志中可以看到这一点:
Actually the class now conforms to none of the protocols. This can be seen in the full compiler log in the Report navigator:
main.swift:24:7: error: type 'SomeClass' does not conform to protocol 'FirstDelegate'
class SomeClass: FirstDelegate, SecondDelegate {
^
main.swift:5:10: note: multiple matching functions named 'someFunc()' with type '() -> ()'
func someFunc()
^
main.swift:19:10: note: candidate exactly matches
func someFunc() {
^
main.swift:13:10: note: candidate exactly matches
func someFunc() {
^
main.swift:24:7: error: type 'SomeClass' does not conform to protocol 'SecondDelegate'
class SomeClass: FirstDelegate, SecondDelegate {
^
main.swift:9:10: note: multiple matching functions named 'someFunc()' with type '() -> ()'
func someFunc()
^
main.swift:19:10: note: candidate exactly matches
func someFunc() {
^
main.swift:13:10: note: candidate exactly matches
func someFunc() {
^