简体   繁体   中英

compare two NSObject in Swift

I want to compare two custom object inherit from NSObject. eg:

class Person: NSObject {
     id: Int!
     ...
}

let a = Person(1)
let b = Person(1)
a == b // false

This is obviously, because they are different object instance. But I want to compare two person according to their id . So I made this change:

class Person {
     id: Int!
}

extension: Equatable {}
func == (lhs: Persion(), rhs: Persion() -> Bool {
     return lhs.id == rhs.id
}

let a = Person(1)
let b = Person(1)
a == b // true

The question is what's the secret of compare two NSObject. What to do if I want compare two Person according to their id while inherit Person from NSObject.

What's the benefit of inherit Person from NSObject? or Is that the correct way to compare two Person object according to their id ?

Any answer will be appreciate!

NSObject has a method named isEqual(object:) that is used for comparing different objects. The default implementation just compares instance pointers, but subclasses can override this method to do custom comparisons.

For example:

class Foo : NSObject {
    var number = 0

    override func isEqual(object: AnyObject?) -> Bool {

        if let otherFoo = object as? Foo {
            return self.number == otherFoo.number
        }
        return false
    }
}

Swift's == operatior automatically uses the isEqual(object:) method when comparing NSObject instances.

let foo1 = Foo()
let foo2 = Foo()
print("Equal: \(foo1 == foo2)") // true

foo1.number = 1
foo2.number = 2
print("Equal: \(foo1 == foo2)") // false

let anotherObject = NSObject()
print("Equal: \(foo1 == anotherObject)") // false

Try like this:

class Person: NSObject {
    // make your person ID immutable 
    let id: Int
    // and a required initialiser
    required init(_ id: Int) {
        self.id = id
    }
}


// make your class Person equatable
func ==(lhs: Person, rhs: Person) -> Bool {
    return lhs.id == rhs.id
}

let a = Person(1)
let b = Person(1)
a == b // true

When comparing instances of objects of a given class, it is always the functional meaning of the class that determines what constitutes equality. It could be any combination of their variables or something much more sophisticated. This has nothing to do with NSObject or native Swift classes.

When you design your class, it is your responsibility to determine what, if anything, constitutes equality. There is no general rule that applies intrinsically to all objects.

If you want it to be an id, just have an id variable and use it in the comparison. But it just as well could have been a combination of name, address and phone number.

At another level of abstraction, there is also the notion of equality of persistence. For example CoreData managedObjects have a unique identifier that corresponds to their storage. So all instances of a managed object that comes from the same place in the database are "equal" from CoreData's perspective because it's function is to centralize changes to a database record. That doesn't mean that there aren't any duplicates of a person's information in the database. Thus the differences in the concept of equality for the various perspectives of the object models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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