Swift 2.0:协议扩展:具有相同功能签名的两个协议编译错误 [英] Swift 2.0: Protocol extensions: Two protocols with the same function signature compile error

查看:133
本文介绍了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'

交换FirstDelegateSecondDelegate:

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() {
         ^

这篇关于Swift 2.0:协议扩展:具有相同功能签名的两个协议编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆