如何修复“无法在扩展中自动合成 'Protocol' 的实现”?

How to fix `Implementation of 'Protocol' cannot be automatically synthesized in an extension`?

我在向扩展添加新协议一致性时遇到编译器错误。

struct EquatableStruct { }

extension EquatableStruct: Equatable {
  static func == (lhs: EquatableStruct, rhs: EquatableStruct) -> Bool {
    return true
  }
}

这里我遇到了编译器错误:

Implementation of 'Equatable' cannot be automatically synthesized in an extension

我该如何解决这个问题?

您错误引用了错误。应该是:

Implementation of 'Equatable' cannot be automatically synthesized in an extension

Comparable 扩展 Equatable。如果您希望您的扩展程序符合 Comparable,您还必须实施 Equatable 协议。

extension ComparableStruct: Comparable {
    static func < (lhs: ComparableStruct, rhs: ComparableStruct) -> Bool {
        return true // FIX
    }

    static func == (lhs: ComparableStruct, rhs: ComparableStruct) -> Bool {
        return true // FIX
    }
}