简体   繁体   English

在swift中比较两个自定义对象

[英]Comparing two custom objects in swift

I have the following protocol defined in Swift: 我在Swift中定义了以下协议:

protocol RecordingObserver {
    func aFunc()
}

Somewhere I have to compare two objects that implement this protocol, to check if they are the same. 某处我必须比较实现此协议的两个对象,以检查它们是否相同。 The problem I'm facing is that apparently Swift doesn't allow us to do this: 我面临的问题是显然Swift不允许我们这样做:

func areEqual(a:RecordingObserver,b:RecordingObserver){
    if a === b {
        println("Equal")
    }
}

Any idea why this is happening? 知道为什么会这样吗? And how I can do this in another way? 我怎么能以另一种方式做到这一点?

=== is the identical to operator and is used to test whether two object references both refer to the same object instance. === 运算符相同,用于测试两个对象引用是否都引用同一个对象实例。 It can be applied only to reference types (ie instances of a class ). 它只能应用于引用类型 (即class实例)。

=== is different from the "equal to" operator == (which is required in the Equatable protocol). ===不同于“等于”运算符== (这在Equatable协议中是必需的)。

Therefore, assuming that 因此,假设

  • the actual observers are instances of a class , and 实际的观察者是一个class实例,和
  • your intention is to check if a and b refer to the same instance , 你的意图是检查ab引用同一个实例

you have to define the protocol as a class protocol : 你必须将协议定义为类协议

protocol RecordingObserver : class {
    // ...
}

Then 然后

func areEqual(a:RecordingObserver,b:RecordingObserver){
    if a === b {
        println("a and b refer to the same object instance")
    }
}

compiles (and works as expected) because the compiler knows that a and b are reference types. 编译(并按预期工作),因为编译器知道ab是引用类型。

Your class needs to support the Equatable protocol to use == 您的类需要支持Equatable协议才能使用==

https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Equatable.html https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Equatable.html

Or if you want to use === something like this... 或者,如果你想使用===这样的东西......

protocol RecordingObserver {
    func aFunc()
}

class MyClass: RecordingObserver {
    func aFunc() {
        // Do something
    }
}

func areEqual(a: MyClass, b: MyClass){
    if a === b {
        println("Equal")
    }
}

I believe there is an 'isEqual' method on NSObject. 我相信NSObject上有一个'isEqual'方法。 If your custom objects are both subclassed from that you should be able to compare a.isEqual(b) . 如果您的自定义对象都是子类,那么您应该能够比较a.isEqual(b)

It is because you said that you objects implement only RecordingObserver . 这是因为你说你的对象只实现了RecordingObserver So compiler don't know if he can compare them. 所以编译器不知道他是否可以比较它们。

Try this: 尝试这个:

func areEqual<T where T: Equatable, T: RecordingObserver>(a: T,b: T) {

}

You can just copy this code into single view project to test: 您只需将此代码复制到单个视图项目中即可进行测试:

protocol RecordingObserver {

}

class SomeClass: NSObject, RecordingObserver {
}

class ViewController: UIViewController {

    func areEqual<T where T: Equatable, T: RecordingObserver>(a: T,b: T) -> Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let a = SomeClass()
        let b = SomeClass()

        NSLog("\(areEqual(a, b: b))")
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM