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

}

swift Equatable 的缺省实现的更多相关文章

  1. swift Equatable 函数签名的测试

    struct Degoo:Equatable { var lex:String var pex:String static func == (left:Degoo, right:Degoo) -> ...

  2. swift class的缺省基类(SwiftObject)与内存模型

    Hard Constraints on Resilience The root of a class hierarchy must remain stable, at pain of invalida ...

  3. 使用Swift打造动态库SDK和DemoAPP时所遇到的(Xcode7.3)

    使用Swift开发SDK的优点是,生成的SDK对于Obj-C或是Swift调用都不需要自己去建桥接文件,因为Swift的SDK打包时默认已经自动生成供OC调用的.h文件.OC调用时直接import,s ...

  4. 分享海量 iOS 及 Mac 开源项目和学习资料

    UI 下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITable ...

  5. swift Hashable Equatable

    /// You can use any type that conforms to the `Hashable` protocol in a set or /// as a dictionary ke ...

  6. Swift mutating Equatable Hashable 待研究

    Swift mutating Equatable Hashable 待研究

  7. 2.Swift教程翻译系列——Swift概览

    英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 依照传统学习程序语言都是从hello,world開始,在Swfit里面仅仅须要一 ...

  8. "Swift"编程语言

    来自英文文档.百度翻译以及自己没过4级的渣渣英语功底,为了自己以后看起来方便 About Swift 关于"海燕" IMPORTANT 重要 This is a prelimina ...

  9. My Swift Study

    参考资源 <swifter> https://github.com/iOS-Swift-Developers/Swift 闭包逃逸 swift3中,闭包默认是非逃逸的.如果一个函数参数可能 ...

随机推荐

  1. 洛谷P1247取火柴游戏

    题目:https://www.luogu.org/problemnew/show/P1247 可以知道必败局面为n[1]^n[2]^...^n[k]=x=0: 而若x不等于0,则一定可以取一次使其变为 ...

  2. web安全之跨站请求伪造

    CSRF(Cross-site request forgery),中文名称:跨站请求伪造.因为这个不是用户真正想发出的请求,这就是所谓的请求伪造;因为这些请求也是可以从第三方网站提交的,所以前缀跨站二 ...

  3. Java递归应用:输出树形菜单

    转自:https://blog.csdn.net/zhangzeyuaaa/article/details/24574769

  4. Java调用Static修饰的本类方法

    public class Dy { public static void main(String[] args){ int a=6; int b=5; int result=0; result=Add ...

  5. 【208】CentOS 下安装 Wine

    参考:centos6.5安装wine 参考:用FreeGate自-由-门软件在Linux上更新Android SDK 参考:CentOS 5.3 使用wine装QQ

  6. A. Bus to Udayland

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. django-ckeditor表情包修改

    一.版本 Django==1.11 django-ckeditor==5.2.2 二.关键步骤 1.删除旧的ckeditor静态文件 所在目录:项目目录下的static文件夹下的ckditor文件夹 ...

  8. 洛谷 - P1361 - 小M的作物 - 最小割 - 最大权闭合子图

    第一次做最小割,不是很理解. https://www.luogu.org/problemnew/show/P1361 要把东西分进两类里,好像可以应用最小割的模板,其中一类A作为源点,另一类B作为汇点 ...

  9. Codeforces - 909C - Python Indentation - 简单dp

    http://codeforces.com/problemset/problem/909/C 好像以前做过,但是当时没做出来,看了题解也不太懂. 一开始以为只有上面的for有了循环体,这里的state ...

  10. Moctf--Pubg题目

    假期举办的一场比赛,开始的题目比较基础,misc神马的都还好说,就是web有些坑了,比如我今天要写的这一道题目.不过大佬说很简单..不过最后我还是解出来了,把思路放在这里. 打开之后得到这个页面,在网 ...