== 和 === 的区别 [英] Difference between == and ===

查看:40
本文介绍了== 和 === 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 swift 中似乎有两个等号运算符:双等号 (==) 和三等号 (====),两者有什么区别?

解决方案

简而言之:

== 运算符检查它们的实例值是否相等,等于"

=== 操作符检查引用是否指向同一个实例,相同于"

长答案:

类是引用类型,多个常量和变量可能在幕后引用同一个类的单个实例.类引用保留在运行时堆栈 (RTS) 中,它们的实例保留在内存的堆区域中.当您使用 == 控制相等性时,这意味着它们的实例是否彼此相等.它不需要是相同的实例才能相等.为此,您需要为自定义类提供相等标准.默认情况下,自定义类和结构不接收等价运算符的默认实现,称为等于"运算符 == 和不等于"运算符 != .为此,您的自定义类需要符合 Equatable 协议,并且它是 static func == (lhs:, rhs:) ->布尔函数

让我们看看例子:

class Person : Equatable {让 ssn: Int让名称:字符串init(ssn: Int, name: String) {self.ssn = ssnself.name = 姓名}static func == (lhs: Person, rhs: Person) ->布尔{返回 lhs.ssn == rhs.ssn}}

P.S.: 由于 ssn(社会安全号码)是唯一号码,因此您无需比较他们的姓名是否相同.

let person1 = Person(ssn: 5, name: "Bob")let person2 = Person(ssn: 5, name: "Bob")如果人 1 == 人 2 {print("两个实例是相等的!")}

尽管person1和person2引用指向Heap区域中两个不同的实例,但它们的实例是相等的,因为它们的ssn编号是相等的.所以输出将是 两个实例是相等的!

if person1 === person2 {//这里不输入} 别的 {print("这两个实例不相同!")}

=== 操作符检查引用是否指向同一个实例,相同于".由于person1和person2在Heap区域有两个不同的实例,所以它们不相同,输出两个实例不相同!

让 person3 = person1

P.S: 类是引用类型,person1 的引用通过这个赋值操作被复制到 person3,因此两个引用指向 Heap 区域中的同一个实例.

if person3 === person1 {打印(两个实例是相同的!")}

它们是相同的,输出将是 两个实例是相同的!

In swift there seem to be two equality operators: the double equals (==) and the triple equals (===), what is the difference between the two?

解决方案

In short:

== operator checks if their instance values are equal, "equal to"

=== operator checks if the references point the same instance, "identical to"

Long Answer:

Classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of a class behind the scenes. Class references stay in Run Time Stack (RTS) and their instances stay in Heap area of Memory. When you control equality with == it means if their instances are equal to each other. It doesn't need to be same instance to be equal. For this you need to provide a equality criteria to your custom class. By default, custom classes and structures do not receive a default implementation of the equivalence operators, known as the "equal to" operator == and "not equal to" operator != . To do this your custom class needs to conform Equatable protocol and it's static func == (lhs:, rhs:) -> Bool function

Let's look at example:

class Person : Equatable {
    let ssn: Int
    let name: String

    init(ssn: Int, name: String) {
        self.ssn = ssn
        self.name = name
    }

    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.ssn == rhs.ssn
    }
}

P.S.: Since ssn(social security number) is a unique number, you don't need to compare if their name are equal or not.

let person1 = Person(ssn: 5, name: "Bob")
let person2 = Person(ssn: 5, name: "Bob")

if person1 == person2 {
   print("the two instances are equal!")
}

Although person1 and person2 references point two different instances in Heap area, their instances are equal because their ssn numbers are equal. So the output will be the two instance are equal!

if person1 === person2 {
   //It does not enter here
} else {
   print("the two instances are not identical!")
}

=== operator checks if the references point the same instance, "identical to". Since person1 and person2 have two different instance in Heap area, they are not identical and the output the two instance are not identical!

let person3 = person1

P.S: Classes are reference types and person1's reference is copied to person3 with this assignment operation, thus both references point the same instance in Heap area.

if person3 === person1 {
   print("the two instances are identical!")
}

They are identical and the output will be the two instances are identical!

这篇关于== 和 === 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆