Crashalot Crashalot - 2 years ago 136
Swift Question

Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject


class below has to override the
function of the

However, calling
on an array of
objects does not cause a breakpoint inside the custom
function to get invoked. Is it possible another
function is overriding this custom one?

Note: Because FooBar must subclass from NSCoding and NSObject, FooBar does not list Equatable as a protocol because it causes this error:

Redundant conformance of 'FooBar' to protocol 'Equatable'

func ==(lhs: FooBar, rhs: FooBar) -> Bool {
return ==

class FooBar: NSObject, NSCoding {
// Class def

// Both serverFooBars and gFooBars are [FooBar]
let newFooBars = serverFooBars.filter { !gFooBars.contains($0) }

Answer Source

Because your class inherits from NSObject you do not need to use the swift protocol Equatable instead you must override the NSObject method isEquals:

class FooBar: NSObject, NSCoding {
  override func isEqual(object: AnyObject?) -> Bool {
    return id == (object as? FooBar)?.id
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download