54

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?

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65
  • *"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 Jun 16 '15 at 13:22
  • 1
    @0x7fffffff `CustomStringConvertible ` is a new protocol in Swift 2 - [see here](http://swiftdoc.org/swift-2/protocol/CustomStringConvertible/). Not sure about `GraphFeature`. – Aaron Brager Jun 16 '15 at 13:24
  • 1
    @AaronBrager: Not really new, just renamed from `Printable`. – Martin R Jun 16 '15 at 14:03

3 Answers3

101

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 2
    In my instance I would love to use the override keyword, but Xcode freaks about overriding overloaded operators (== for Equatable). Any ideas? – rob5408 Sep 26 '15 at 19:51
  • it's not necessary to use the override keyword. See the section "Equivalence Operators" in https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_43 – Max MacLeod Jan 13 '16 at 09:58
  • @MaxMacLeod if not using the override keyword how do you ensure a custom implementation of == gets called? http://stackoverflow.com/questions/37085839/overridden-function-for-equatable-type-not-called-for-custom-class – Crashalot May 07 '16 at 07:45
  • 1
    So which class is it actually inheriting, I mean the class inheriting the `CustomDebugStringConvertible` – arqam Nov 28 '16 at 04:25
9

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.

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
  • 1
    It might happen that you can't set your file's target only for the current app (and not also the tests). If this happens, separate the file into two files, one that contains the redundant protocol conformance (make it current target only) and one that has the rest of the code (set its target to both your app and app tests). – Teodor Ciuraru Aug 06 '16 at 21:42
  • 2
    This answer helped me realise I had the source file being tested added as a member of both my App and the Testing target. Removing it as a member of the testing target resolved the error. Strange though that the error appeared suddenly after this being the case for a long time. I'm not exactly sure what the trigger was, as I'd been running tests (very similar ones too) for ages on the prior ownership setup. – Rob Glassey Aug 25 '16 at 15:15
2

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! :-)

Vitya Shurapov
  • 2,200
  • 2
  • 27
  • 32