在Swift 5中,比较两个闭包的指针的方法是什么? [英] In Swift 5, what is a way to compare pointers to two closures?

查看:43
本文介绍了在Swift 5中,比较两个闭包的指针的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 5中,比较两个闭包的指针的方法是什么?

In Swift 5, what is a way to compare pointers to two closures?

有人告诉我这里在Swift 3中,进行比较的方法是什么两个闭包?

这在某种程度上是一个不同的问题.所以现在您有了它.

that this is somehow a different question. So now you have it.

public typealias Action = () -> Void

let action1: Action = {}
let action2: Action = {}

assert(action1 == action1)
assert(action1 != action2)

推荐答案

关于指针:值得注意的是,对象(类)是引用类型,因此两个对象的相等性与标识是不同的两个对象.对于相等性(如果在类中定义),您具有 == 运算符,对于身份而言,您具有 === 运算符:

About pointers: notably, objects (classes) are reference types, so the equality of two objects is not the same as the identity of two objects. For equality (if defined within the class), you have the == operator, and for identity, the === operator:

class Foo:Equatable {
    var i:Int = 0
    static func == (lhs: Foo, rhs: Foo) -> Bool {
        return lhs.i == rhs.i
    }
}

let foo1 = Foo()
let foo2 = Foo()
let referenceToFoo1 = foo1

foo1 == foo2 // equality: true, since foo1.i == foo2.i, which is the criteria for equality
foo1 === foo2 // identity: false, since they are two different instances
foo1 === referenceToFoo1 // identity: true, both variables point to the same instance
foo1.i = 2
print(referenceToFoo1.i) // 2, since it refers to the same object

参考"也可以称为指针".尽管在Swift中,与C语言不同,我们不必深入研究指针算法,这是一种处理对象和其他数据的指针的内存地址的相当低级的方法.

"Reference" can also be called a "pointer," although in Swift, unlike in C, we don't have to delve into pointer arithmetic, which is a pretty low-level way of dealing with memory addresses of pointers to objects and other data.

就像类一样,闭包也是Swift中的引用类型,因此除了它们的平等"之外,(它们是否包含相同的代码和捕获的信息等),我们还可以查看它们的身份(即,这两个变量是指同一个闭包还是两个不同的闭包,即使它们看起来相同,等等).

Just like classes, closures are also reference types in Swift, so in addition to their "equality" (do they contain the same code and captured information etc.) we can also look into their identity (i.e. do these two variables refer to the same closure or two different ones, even if they look the same etc.).

问题是,Swift似乎并不想在这里帮助我们.

Problem is, Swift doesn't really seem to want to help us there.

就像 == 不适用于闭包一样, === 也不适用.闭包类型似乎都不符合任何协议(请注意,没有单一的闭包类型",而是取决于参数和返回类型的数量众多).

Just like == doesn't work for closures, neither does ===. Neither do closure-types seem to conform to any protocol (and note that there's no single "closure type" but rather an infinite multitude of them depending on parameters and return types).

此外,即使将闭包强制转换为 AnyObject 也不起作用:

Furthermore, even casting a closure to AnyObject doesn't work:

foo1 as AnyObject === referenceToFoo1 as AnyObject // true, as expected

// with the `action1` closure from the question:
action1 as AnyObject === action1 as AnyObject // false; a bit of a surprise

看起来就像Swift每次对 AnyObject 强制关闭时,由于某种原因,它都会创建一个新的 AnyObject 实例...因此比较这些内容也没有发现任何结果.

Looks like every time Swift casts a closure to AnyObject, it creates a new AnyObject instance for some reason… So comparing these also reveals nothing.

所以……我认为我们无法对Swift中闭包的标识或相等进行推理.也许还有其他方法,可能通过不安全的指针……好吧,让我知道是否任何人都可以从那个兔子洞里走下来!

So… I don't think we can reason about the identity or equality of closures in Swift. Maybe there is some other way, possibly via unsafe pointers… well, let me know if anyone had any luck going down that rabbit hole!

这篇关于在Swift 5中,比较两个闭包的指针的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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