NSObject Prefer isEqual

NSObject subclasses should implement isEqual instead of ==

  • Identifier: nsobject_prefer_isequal
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: lint
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning

Non Triggering Examples

class AClass: NSObject {
}
@objc class AClass: SomeNSObjectSubclass {
}
class AClass: Equatable {
    static func ==(lhs: AClass, rhs: AClass) -> Bool {
        return true
    }
class AClass: NSObject {
    override func isEqual(_ object: Any?) -> Bool {
        return true
    }
}
@objc class AClass: SomeNSObjectSubclass {
    override func isEqual(_ object: Any?) -> Bool {
        return false
    }
}
class AClass: NSObject {
    static func ==(lhs: AClass, rhs: BClass) -> Bool {
        return true
    }
}
struct AStruct: Equatable {
    static func ==(lhs: AStruct, rhs: AStruct) -> Bool {
        return false
    }
}
enum AnEnum: Equatable {
    static func ==(lhs: AnEnum, rhs: AnEnum) -> Bool {
        return true
    }
}

Triggering Examples

class AClass: NSObject {
    static func ==(lhs: AClass, rhs: AClass) -> Bool {
        return false
    }
}
@objc class AClass: SomeOtherNSObjectSubclass {
    static func ==(lhs: AClass, rhs: AClass) -> Bool {
        return true
    }
}
class AClass: NSObject, Equatable {
    static func ==(lhs: AClass, rhs: AClass) -> Bool {
        return false
    }
}
class AClass: NSObject {
    override func isEqual(_ object: Any?) -> Bool {
        guard let other = object as? AClass else {
            return false
        }
        return true
    }

    static func ==(lhs: AClass, rhs: AClass) -> Bool {
        return false
    }
}