2

In my Swift app I have a 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 {

}

Now, I'm creating an array that stores some objects of that type:

var array:[CustomCluster] = []

and append objects like this:

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);
   }

The problem is, that even in case when coordinate and amount is the same as in a previously added cluster, this line:

if(!self.array.contains(pinOne))

doesn't work well and allow adding another pin to the array (even though there is already a pin with the same exact data).

How can I exclude new objects with the same coordinates and amount from adding them to my array?

===

So I just added a function:

static func == (lcc: CustomCluster, rcc: CustomCluster) -> Bool {
    return
        lcc.coordinate.latitude == rcc.coordinate.latitude &&
            lcc.coordinate.longitude == rcc.coordinate.longitude &&
            lcc.amount == rcc.amount
}

but the problem still occurs, is there anything else that I'm missing here?

Hamish
  • 78,605
  • 19
  • 187
  • 280
user3766930
  • 5,629
  • 10
  • 51
  • 104
  • 1
    You need to make CustomCluster conform to Equatable protocol – Leo Dabus Mar 21 '17 at 22:38
  • @LeoDabus could you please check the edit to my question? – user3766930 Mar 21 '17 at 22:46
  • Do you need to subclass NSObject or MKAnnotation? Doesn't look like you need so just remove it – Leo Dabus Mar 21 '17 at 22:48
  • hm I need it to conform to protocol of `MKAnnotation`, so I guess it has to conform to NSObject too? I need it later in my code... – user3766930 Mar 21 '17 at 22:51
  • Later on I'm using the array to display annotations on my map view with a method: `open func displayAnnotations(_ annotations: [MKAnnotation], onMapView mapView:MKMapView){` – user3766930 Mar 21 '17 at 22:56
  • 1
    Given you're subclassing `NSObject`, you need to override `isEqual(_:)` and `hash` – see for example http://stackoverflow.com/q/33319959/2976878 – Hamish Mar 21 '17 at 22:59

1 Answers1

2

As already mentioned in comments also by Hamish when subclassing NSObject you need to override isEqual method when conforming to Equatable protocol. Try like this:

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
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571