博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift Equatable 的缺省实现
阅读量:6413 次
发布时间:2019-06-23

本文共 1249 字,大约阅读时间需要 4 分钟。

Starting from Swift 4.1, all you have to is to conform to the  protocol without the need of implementing the == method. See: .

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

}

 

转载地址:http://gidra.baihongyu.com/

你可能感兴趣的文章
html5-离线缓存
查看>>
linux系统安装完后的常见工作
查看>>
在Linux服务器、客户端中构建密钥对验证进行远程连接
查看>>
揪出MySQL磁盘消耗迅猛的真凶
查看>>
和“C”的再遇
查看>>
一键安装kubernetes 1.13.0 集群
查看>>
Java内存模型
查看>>
第一讲 机器学习中的数学
查看>>
RabbitMq的集群搭建
查看>>
asp.net web常用控件FileUpload(文件上传控件)
查看>>
动态网页的建立
查看>>
参数展开与特殊字符
查看>>
spring boot + mybatis 同时访问多数据源
查看>>
URL中汉字转码
查看>>
搭建TurnServer服务器
查看>>
转载:PHP性能提升之OPcache相关参数详解
查看>>
[转]Tutorial about USB HID Report Descriptors
查看>>
[转]go正则实例
查看>>
Selector中关于顺序的注意事项
查看>>
font-size: 62.5% 的含义
查看>>