Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

swift2 - Swift 2.0: Protocol extensions: Two protocols with the same function signature compile error

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")
    }
}

and trying to conform to both of them:

class SomeClass: FirstDelegate, SecondDelegate {}

I receive compile-time error:

Type 'SomeClass' does not conform to protocol 'FirstDelegate'

Exchanging FirstDelegate and SecondDelegate:

class SomeClass: SecondDelegate, FirstDelegate {}

produces reverse:

Type 'SomeClass' does not conform to protocol 'SecondDelegate'

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A protocol defines requirements (methods, properties, ...) for a conformant type.

protocol FirstDelegate {
    func someFunc()
}

protocol SecondDelegate {
    func 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")
    }
}

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

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

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...