• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swiftEquatable的缺省实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift3.0之cell的三种创建方式发布时间:2022-07-13
下一篇:
Swift - 使用MapKit显示地图,并在地图上做标记发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap