在我Swift
应用程序我有一个类:如何检查我的NSObject数组是否在Swift中包含特定的NSObject?
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
}
,但问题仍然存在,还有什么是我在这里失踪?
您需要使CustomCluster符合Equatable协议 –
@LeoDabus请问您可以检查编辑我的问题吗? – user3766930
您是否需要继承NSObject或MKAnnotation?看起来你不需要这样只是删除它 –