Starting from Swift 4.1, all you have to is to conform to the Equatable protocol without the need of implementing the == method. See: SE-0185 - Synthesizing Equatable and Hashable conformance.

Example:


Keep in mind that the default behavior for the == is to compare all the type properties (based on the example: lhs.id == rhs.id && lhs.value == rhs.value). If you are aiming to achieve a custom behavior (comparing only one property for instance), you have to do it by yourself:

At this point, the equality would be based on the id value, regardless of what's the value of value.

 

https://stackoverflow.com/questions/37541512/swift-struct-doesnt-conform-to-protocol-equatable

 

猜测:下面两者的签名形式应该是相同的

 

/// Represents a response to a `MoyaProvider.request`.

public final class Response: CustomDebugStringConvertible, Equatable {

    public static func == (lhs: Response, rhs: Response) -> Bool {

        return lhs.statusCode == rhs.statusCode

            && lhs.data == rhs.data

            && lhs.response == rhs.response

    }

}

 

public func ==(lhs: ConstraintItem, rhs: ConstraintItem) -> Bool {

    // pointer equality

    guard lhs !== rhs else {

        return true

    }

    

    // must both have valid targets and identical attributes

    guard let target1 = lhs.target,

          let target2 = rhs.target,

          target1 === target2 && lhs.attributes == rhs.attributes else {

            return false

    }

    

    return true

}

 

------------------越是喧嚣的世界,越需要宁静的思考------------------ 合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下。 积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里;不积小流,无以成江海。骐骥一跃,不能十步;驽马十驾,功在不舍。锲而舍之,朽木不折;锲而不舍,金石可镂。蚓无爪牙之利,筋骨之强,上食埃土,下饮黄泉,用心一也。蟹六跪而二螯,非蛇鳝之穴无可寄托者,用心躁也。