s3协议和swift协议_Swift中的Equatable协议是什么?

s3协议和swift协议

Today we will learn about a protocol that provides a clear way to find out if your custom objects are equal.

今天,我们将学习一种协议,该协议提供了一种清晰的方法来找出您的自定义对象是否相等。

Without further ado, let’s explore Equatable protocol in practice.

Equatable ,让我们探索Equatable协议 在实践中。

开始吧 (Let’s Start)

Say we have simple struct called StudentsBook:

假设我们有一个简单的结构,称为StudentsBook

But how can we make sure it’s the right student? In order to compare instances for equality we need to:

但是我们如何确保它是合适的学生呢? 为了比较实例是否相等,我们需要:

  • Adopt Equatable protocol

    采用平等协议
  • Provide an implementation for the == operator. Simply add a static == func that takes lhs (left-hand side) and rhs (right-hand side) parameters and returns a Bool that decides whether the two sides are equal or not.

    提供==运算符的实现。 只需添加一个带有lhs (左侧)和rhs (右侧)参数的静态== func,然后返回一个Bool确定两个边是否相等。

By doing this, we’ve compared all instances and get a pure result.

通过这样做,我们比较了所有实例并获得了纯结果。

However, in certain cases, there are requirements of a protocol that can be autogenerated for you. You only specify the name of protocol and don’t write == method on your own. The Swift compiler will autogenerate the implementation just by looking at data types.

但是,在某些情况下,可能需要为您自动生成协议的要求。 您仅指定协议的名称,而不自己编写==方法。 Swift编译器仅通过查看数据类型即可自动生成实现。

结语 (Wrapping up)

This is all you should know about Equatable protocol. I hope it was interesting!

这是您关于Equatable协议应了解的全部信息。 我希望这很有趣!

Interested in other relative protocols? Feel free to visit my other relevant articles:

对其他相关协议感兴趣吗? 随时访问我的其他相关文章:

If you have any criticism, question, or suggestion, feel free to post them in the comment section below!

如果您有任何批评,问题或建议,请随时在下面的评论部分中发表!

Thanks for reading.

谢谢阅读。

翻译自: https://medium.com/cleansoftware/what-is-the-equatable-protocol-in-swift-3cced3f28219

s3协议和swift协议

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值