如何检查我的 NSObjects 数组是否包含 Swift 中的特定 NSObject?
how can I check if my array of NSObjects contains a specific NSObject in Swift?
在我的 Swift
应用程序中,我有一个 class:
open class CustomCluster : NSObject {
open var coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
open var title: String? = ""
open var amount: Int = 0
}
extension CustomCluster : MKAnnotation {
}
现在,我正在创建一个数组来存储该类型的一些对象:
var array:[CustomCluster] = []
并像这样附加对象:
let pinOne = CustomCluster()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.amount = request.amount
if(!self.array.contains(pinOne))
{
self.array.append(pinOne);
}
问题是,即使坐标和数量与之前添加的集群相同,这一行:
if(!self.array.contains(pinOne))
不能很好地工作并允许向数组添加另一个引脚(即使已经有一个引脚具有完全相同的数据)。
如何排除具有相同坐标和数量的新对象将它们添加到我的数组中?
===
所以我刚刚添加了一个函数:
static func == (lcc: CustomCluster, rcc: CustomCluster) -> Bool {
return
lcc.coordinate.latitude == rcc.coordinate.latitude &&
lcc.coordinate.longitude == rcc.coordinate.longitude &&
lcc.amount == rcc.amount
}
但问题仍然存在,还有什么我在这里遗漏的吗?
正如 Hamish 在评论中已经提到的,当子类化 NSObject 时,您需要在符合 Equatable 协议时重写 isEqual 方法。像这样尝试:
class CustomCluster: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
var title: String? = ""
var amount: Int = 0
override func isEqual(_ object: Any?) -> Bool {
return coordinate.latitude == (object as? CustomCluster)?.coordinate.latitude &&
coordinate.longitude == (object as? CustomCluster)?.coordinate.longitude &&
title == (object as? CustomCluster)?.title &&
amount == (object as? CustomCluster)?.amount
}
}
在我的 Swift
应用程序中,我有一个 class:
open class CustomCluster : NSObject {
open var coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
open var title: String? = ""
open var amount: Int = 0
}
extension CustomCluster : MKAnnotation {
}
现在,我正在创建一个数组来存储该类型的一些对象:
var array:[CustomCluster] = []
并像这样附加对象:
let pinOne = CustomCluster()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.amount = request.amount
if(!self.array.contains(pinOne))
{
self.array.append(pinOne);
}
问题是,即使坐标和数量与之前添加的集群相同,这一行:
if(!self.array.contains(pinOne))
不能很好地工作并允许向数组添加另一个引脚(即使已经有一个引脚具有完全相同的数据)。
如何排除具有相同坐标和数量的新对象将它们添加到我的数组中?
===
所以我刚刚添加了一个函数:
static func == (lcc: CustomCluster, rcc: CustomCluster) -> Bool {
return
lcc.coordinate.latitude == rcc.coordinate.latitude &&
lcc.coordinate.longitude == rcc.coordinate.longitude &&
lcc.amount == rcc.amount
}
但问题仍然存在,还有什么我在这里遗漏的吗?
正如 Hamish 在评论中已经提到的,当子类化 NSObject 时,您需要在符合 Equatable 协议时重写 isEqual 方法。像这样尝试:
class CustomCluster: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
var title: String? = ""
var amount: Int = 0
override func isEqual(_ object: Any?) -> Bool {
return coordinate.latitude == (object as? CustomCluster)?.coordinate.latitude &&
coordinate.longitude == (object as? CustomCluster)?.coordinate.longitude &&
title == (object as? CustomCluster)?.title &&
amount == (object as? CustomCluster)?.amount
}
}