Swift Array.contains()不调用PFUser子类的Equatable函数

时间:2016-01-03 18:33:21

标签: arrays swift parse-platform pfuser

我有一个 PFUser - MYUser 类的子类,实现了 Equatable 函数,用于以这种方式比较objectIds:

func ==(left: MYUser, right: MYUser) -> Bool {
    return left.objectId == right.objectId
}

但是当我调用Array.contains()方法时,它并没有调用 Equatable 函数的这种实现,这会导致错误的结果。例如,这里:

let hasUser = self.selectedUsers.contains(currentUser)

hasUser 变为 false 如果 selectedUsers 数组包含不同的内存对象但具有与 currentUser 中相同的objectId。

在直接使用中调用有趣的, Equatable 函数实现。这里:

var hasUser = false
for itUser in self.selectedUsers {
  if itUser == currentUser {
    hasUser = true
    break
  }
}
已成功调用

== 运算符, hasUser 具有不同内存对象的正确值,但具有相同的objectId

可能是什么原因造成的?

更新。 这是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
}

1 个答案:

答案 0 :(得分:12)

我认为这是NSObject问题。

class 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, right: MYUserNSObject) -> Bool {
    return left.fullName == right.fullName
}

let objectUsers = [MYUserNSObject(fullName: "a"), 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)")

打印真实。

注意:不再需要==(left:right:)功能。

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)")