gpt4 book ai didi

ios - CIImage 应该是平等的吗?

转载 作者:行者123 更新时间:2023-11-28 12:44:20 24 4
gpt4 key购买 nike

因此,Apple 的文档说 CIImage 符合 Equatable。我认为这意味着以下单元测试将通过。然而,事实并非如此。我很想知道为什么。

func test_CIImageEqualityShouldWork() {
let bundle = NSBundle(forClass: PrototypeTests.self)
guard let path = bundle.pathForResource("testImage", ofType: "png") else { return }
guard let image = UIImage(contentsOfFile: path) else { return }

let thingy1 = CIImage(image: image)
let thingy2 = CIImage(image: image)
XCTAssert(thingy1 == thingy2)
}

图像存在,guard 语句都通过了,但是 assert 失败了,它们不相等。

出于兴趣,我尝试创建了两次 UIImage 并进行了比较。那也失败了。

最佳答案

所有NSObject子类都符合Equatable==函数在对象上调用 isEqual: 方法。 isEqual: 方法NSObject 只是比较对象指针,即 o1 == o2如果 o1o2 引用相同的对象实例则成立。

例子见 Interacting with Objective-C APIs :

Swift provides default implementations of the == and === operators and adopts the Equatable protocol for objects that derive from the NSObject class. The default implementation of the == operator invokes the isEqual: method, and the default implementation of the === operator checks pointer equality. You should not override the equality or identity operators for types imported from Objective-C.

The base implementation of the isEqual: provided by the NSObject class is equivalent to an identity check by pointer equality.

许多 NSObject 子类覆盖了 isEqual: 方法(例如 NSStringNSArray, NSDate, ...) 但不是 CIImage:

let thingy1 = CIImage(image: image)
let thingy2 = CIImage(image: image)

创建两个不同的 CIImage 实例并将它们比较为“不相等”。

关于ios - CIImage 应该是平等的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38574640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com