数组不相等但实际上是?
Array is not equal but it actually is?
这太疯狂了。我正在比较自定义类型的两个数组,但它说它不相等但实际上是,因为我打印了其中的所有对象并用 https://www.diffchecker.com
检查它们是否不同,但它们不相等!我正在使用 Swift 4.2
func deleteFavorite(_ favorite: Favorite) {
var oldFavorites = getFavoriteSurahs()
oldFavorites.forEach {
[=10=].reciter.surahs.forEach {
print([=10=].name)
print([=10=].number)
}
print()
favorite.reciter.surahs.forEach {
print([=10=].name)
print([=10=].number)
}
print(NSArray(array:
[=10=].reciter.surahs).isEqual(favorite.reciter.surahs))
// It prints false BUT IT SHOULD PRINT TRUE, BECAUSE THEY'RE ACTUALLY EQUAL
}
}
那是我的古兰经 class:
class Surah: NSObject, NSCoding {
private(set) public var name: String
private(set) public var number: Int
init(number: Int, name: String) {
self.number = number
self.name = name
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.number, forKey: "number")
}
required init?(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObject(forKey: "name") as! String
self.number = aDecoder.decodeInteger(forKey: "number")
}
}
这是我的 getFavoriteSurahs()
函数:
func getFavoriteSurahs() -> [Favorite] {
if let favoritesData = self.object(forKey: UD_FAVORITES) as? Data {
do {
let favorites = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(favoritesData) as! [Favorite]
return favorites
} catch {
debugPrint(error)
return [Favorite]()
}
} else {
print("EMPTY Favorites")
return [Favorite]()
}
}
UPDATE
This is so dump:
print([=13=].surah.number == favorite.surah.number)
print([=13=].surah.name == favorite.surah.name)
// Prints true
print([=13=].surah == favorite.surah)
// Prints false
UPDATE 2
if [=14=].reciter.name == favorite.reciter.name && [=14=].reciter.image == favorite.reciter.image && [=14=].reciter.documentID == favorite.reciter.documentID &&
[=14=].reciter.surahs.count == favorite.reciter.surahs.count &&
[=14=].surah.number == favorite.surah.number &&
[=14=].surah.name == favorite.surah.name {
print("OMG FINALLY")
} else {
print("NOO NOO NOO")
}
// Prints OMG FINALLY. After checking every single item. Is this normal??
NSArray
是引用类型,因此您正在检查两个 NSArray
指针是否相等。您应该使用本机 Swift Array
类型,它可以满足您的需求。
如果您将 NSArray 的 isEqual 用于自定义对象(不是来自 SDK),那么您需要为该类型的对象实现 isEqual。类似的东西:
override func isEqual(object: AnyObject?) -> Bool {
if let object = object as? MyClass {
// check for every ivar that should be equal
return foo == object.foo && bar == object.bar
} else {
return false
}
}
您正在子类化 NSObject
,它实现了 Equatable
协议,但默认情况下仅使用指针相等性。您需要覆盖 isEqual(_:)
方法和 hash
属性:
override func isEqual(_ object: Any?) -> Bool {
guard let rhs = object as? Surah else { return false }
return number == rhs.number && name == rhs.name
}
override var hash: Int {
return number.hashValue ^ name.hashValue
}
let s1 = Surah(number: 1, name: "One")
let s2 = Surah(number: 1, name: "One")
s1 == s2 // => true
[s1] == [s2] // => true
这太疯狂了。我正在比较自定义类型的两个数组,但它说它不相等但实际上是,因为我打印了其中的所有对象并用 https://www.diffchecker.com
检查它们是否不同,但它们不相等!我正在使用 Swift 4.2
func deleteFavorite(_ favorite: Favorite) {
var oldFavorites = getFavoriteSurahs()
oldFavorites.forEach {
[=10=].reciter.surahs.forEach {
print([=10=].name)
print([=10=].number)
}
print()
favorite.reciter.surahs.forEach {
print([=10=].name)
print([=10=].number)
}
print(NSArray(array:
[=10=].reciter.surahs).isEqual(favorite.reciter.surahs))
// It prints false BUT IT SHOULD PRINT TRUE, BECAUSE THEY'RE ACTUALLY EQUAL
}
}
那是我的古兰经 class:
class Surah: NSObject, NSCoding {
private(set) public var name: String
private(set) public var number: Int
init(number: Int, name: String) {
self.number = number
self.name = name
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.number, forKey: "number")
}
required init?(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObject(forKey: "name") as! String
self.number = aDecoder.decodeInteger(forKey: "number")
}
}
这是我的 getFavoriteSurahs()
函数:
func getFavoriteSurahs() -> [Favorite] {
if let favoritesData = self.object(forKey: UD_FAVORITES) as? Data {
do {
let favorites = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(favoritesData) as! [Favorite]
return favorites
} catch {
debugPrint(error)
return [Favorite]()
}
} else {
print("EMPTY Favorites")
return [Favorite]()
}
}
UPDATE This is so dump:
print([=13=].surah.number == favorite.surah.number)
print([=13=].surah.name == favorite.surah.name)
// Prints true
print([=13=].surah == favorite.surah)
// Prints false
UPDATE 2
if [=14=].reciter.name == favorite.reciter.name && [=14=].reciter.image == favorite.reciter.image && [=14=].reciter.documentID == favorite.reciter.documentID &&
[=14=].reciter.surahs.count == favorite.reciter.surahs.count &&
[=14=].surah.number == favorite.surah.number &&
[=14=].surah.name == favorite.surah.name {
print("OMG FINALLY")
} else {
print("NOO NOO NOO")
}
// Prints OMG FINALLY. After checking every single item. Is this normal??
NSArray
是引用类型,因此您正在检查两个 NSArray
指针是否相等。您应该使用本机 Swift Array
类型,它可以满足您的需求。
如果您将 NSArray 的 isEqual 用于自定义对象(不是来自 SDK),那么您需要为该类型的对象实现 isEqual。类似的东西:
override func isEqual(object: AnyObject?) -> Bool {
if let object = object as? MyClass {
// check for every ivar that should be equal
return foo == object.foo && bar == object.bar
} else {
return false
}
}
您正在子类化 NSObject
,它实现了 Equatable
协议,但默认情况下仅使用指针相等性。您需要覆盖 isEqual(_:)
方法和 hash
属性:
override func isEqual(_ object: Any?) -> Bool {
guard let rhs = object as? Surah else { return false }
return number == rhs.number && name == rhs.name
}
override var hash: Int {
return number.hashValue ^ name.hashValue
}
let s1 = Surah(number: 1, name: "One")
let s2 = Surah(number: 1, name: "One")
s1 == s2 // => true
[s1] == [s2] // => true