参见英文答案 >
NSObject subclass in Swift: hash vs hashValue,isEqual vs ==2个
我有一个PFUser的子类 – MYUser类,带有Equatable函数的实现,用于以这种方式比较objectIds:
我有一个PFUser的子类 – MYUser类,带有Equatable函数的实现,用于以这种方式比较objectIds:
func ==(left: MYUser,right: MYUser) -> Bool { return left.objectId == right.objectId }
但是当我调用Array.contains()方法时,它不会调用Equatable函数的这个实现,这会导致不正确的结果.例如,这里:
let hasUser = self.selectedUsers.contains(currentUser)
如果selectedUsers数组包含不同的内存对象但具有与currentUser中相同的objectId,则hasUser变为false.
有趣的是,Equatable函数实现在直接使用中被调用.这里:
var hasUser = false for itUser in self.selectedUsers { if itUser == currentUser { hasUser = true break } }
==运算符成功调用,并且hasUser具有不同内存对象的正确值,但具有相同的objectId
它可能是什么原因?
UPDATE.
这是MYUser课程:
class MYUser: PFUser { // MARK: - Parse Object @NSManaged var avatarFile: PFFile? @NSManaged var fullName: String? // MARK: - PFSubclassing Methods (through PFUser) override class func initialize() { struct Static { static var onceToken : dispatch_once_t = 0; } dispatch_once(&Static.onceToken) { self.registerSubclass() } } } func ==(left: MYUser,right: MYUser) -> Bool { return left.objectId == right.objectId }
我认为这是一个NSObject问题.
原文链接:https://www.f2er.com/swift/319227.htmlclass MYUserNSObject: NSObject { dynamic var fullName: String init(fullName: String) { self.fullName = fullName super.init() } } func ==(left: MYUserNSObject,right: MYUserNSObject) -> Bool { return left.fullName == right.fullName } let objectUsers = [MYUserNSObject(fullName: "a"),MYUserNSObject(fullName: "b")] let objectResult = objectUsers.contains(MYUserNSObject(fullName: "a")) print("\(result)")
打印错误.
class MYUserSwift: Equatable { var fullName: String init(fullName: String) { self.fullName = fullName } } func ==(left: MYUserSwift,right: MYUserSwift) -> Bool { return left.fullName == right.fullName } let swiftUsers = [MYUserSwift(fullName: "a"),MYUserSwift(fullName: "b")] let swiftResult = swiftUsers.contains(MYUserSwift(fullName: "a")) print("\(swiftResult)")
打印真实.
最后,通过添加-isEqual:,我修复了这个问题.
class MYUserNSObject: NSObject { dynamic var fullName: String init(fullName: String) { self.fullName = fullName super.init() } override func isEqual(object: AnyObject?) -> Bool { guard let user = object as? MYUserNSObject else { return false } return self == user } } func ==(left: MYUserNSObject,MYUserNSObject(fullName: "b")] let objectResult = objectUsers.contains(MYUserNSObject(fullName: "a")) print("\(objectResult)")
打印真实.
针对Swift 4.0进行了更新
class MYUserNSObject: NSObject { @objc var fullName: String init(fullName: String) { self.fullName = fullName super.init() } override func isEqual(_ object: Any?) -> Bool { guard let user = object as? MYUserNSObject else { return false } return self.fullName == user.fullName } } let objectUsers = [MYUserNSObject(fullName: "a"),MYUserNSObject(fullName: "b")] let objectResult = objectUsers.contains(MYUserNSObject(fullName: "a")) print("\(objectResult)")
打印真实.
注意:不再需要==(左:右:)函数.
let success = MYUserNSObject(fullName: "a") == objectUsers[0] print("success should be true: \(success)") let failure = MYUserNSObject(fullName: "a") == objectUsers[1] print("failure should be false: \(failure)")