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

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

问题描述

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

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

推荐答案

简而言之:

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

=== 运算符检查引用是否指向同一个实例,"identical to"

长答案:

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

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

我们来看例子:

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.: 由于 ssn(social security number) 是唯一的号码,所以你不需要比较他们的名字是否相同.

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!")
}

虽然 person1 和 person2 引用指向 Heap 区域中的两个不同实例,但它们的实例是相等的,因为它们的 ssn 编号相等.所以输出将是 the two instance 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!")
}

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

let person3 = person1

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

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天全站免登陆