52
votes

I updated my project to Swift 2, and received a bunch of redundant conformance of XXX to protocol YYY. This happens especially often (or always) when a class conforms to CustomStringConvertible. Also some place with Equatable.

class GraphFeatureNumbersetRange: GraphFeature, CustomStringConvertible { // <--- get the error here
...
}

I suspect that I don't need to explicitly conform to a protocol when I implement var description: String { get }, or whatever methods the protocol requires. Should I just follow fixit instructions and remove all these? Does Swift now automatically infer the conformance if a class implements all the protocol's methods?

3
"I suspect that I don't need to explicitly conform to a protocol"… this is incorrect. If you want to interact with a protocol, you must explicitly conform to it. "Should I just follow fixit instructions?" You need to add more detail to your question so we can reproduce the issue. "Does Swift now automatically infer the conformance if a class implements all the protocol's methods?" No.Aaron Brager
@0x7fffffff CustomStringConvertible is a new protocol in Swift 2 - see here. Not sure about GraphFeature.Aaron Brager
@AaronBrager: Not really new, just renamed from Printable.Martin R

3 Answers

96
votes

You'll get that error message in Xcode 7 (Swift 2) if a subclass declares conformance to a protocol which is already inherited from a superclass. Example:

class MyClass : CustomStringConvertible {
    var description: String { return "MyClass" }
}

class Subclass : MyClass, CustomStringConvertible {
    override var description: String { return "Subclass" }
}

The error log shows:

main.swift:10:27: error: redundant conformance of 'Subclass' to protocol 'CustomStringConvertible'
class Subclass : MyClass, CustomStringConvertible {
                          ^
main.swift:10:7: note: 'Subclass' inherits conformance to protocol 'CustomStringConvertible' from superclass here
class Subclass : MyClass, CustomStringConvertible {
      ^

Removing the protocol conformance from the subclass declaration solves the problem:

class Subclass : MyClass {
    override var description: String { return "Subclass" }
}

But the superclass must declare the conformance explicitly, it is not automatically inferred from the existence of the description property.

6
votes

For googlers, I also got this error when including SwiftyJson in my Tests target and adding a swift test class, as it caused SwiftyJson to be compiled in again, and it declares NSNumber as Comparable. The solution was to only include it in the app target.

1
votes

The point is that your GraphFeatureNumbersetRange is NSObject's subclass. Which in its turn already conforms to CustomStringConvertible! That's it! Just delete this redundant protocol. Now you're declaring it twice! :-)