0

I have this problem and I'm newer to Swift. I have tried looking around on the internet and found stuff which looked right but for some reason I can't use it. This is the snippet I am using:

    /** This struct is meant to hold all of the information taken from the firebase database */
    struct Service {
    var name: String
    var description: String
    var hours: String
    var type: String
    var estimated_payment: String
    var ge_min_payment: String
    var hourly_rate: String
    var income_category: String
    var part_cost: String
    var part_markup: String
    var standard_price: String
    var task_number: String
    var ttsp_price: String
    var ttsp_savings: String
    var agreement_discount: String
    var annual_part_increase: String
}

  // ...

  /** cartArray contains all the objects currently in the cart for checkout */
  var cartArray: [Service] = []

  // ....  

  /**
    * isInCart
    *
    * a boolean function which checks to see if a specific service object is in the array
    */
    func isInCart(service: Service) -> Bool {
        var contained = self.cartArray.contains(service)
        return contained ? true : false;
    }

When I try to compile this project, this is the error I am receiving:

enter image description here

I'm a little confused on what is wrong with it. Could someone help?

booky99
  • 1,436
  • 4
  • 27
  • 45
  • 7
    This has been answered before: http://stackoverflow.com/questions/24102024/how-to-check-if-an-element-is-in-an-array/33532399#33532399 – blau May 03 '16 at 22:05
  • 4
    Basically there are two `contains` functions – one asks for you to provide a closure with custom comparison logic to check a given element with your target element & the other just takes an element and checks it itself. The difference is that latter requires the element to conform to the `Equatable` protocol. You either need to conform your `Service` with `Equatable` – or use a closure (the former is preferred, as you should always make your value types equatable). – Hamish May 03 '16 at 22:09
  • @originaluser2, should make that an answer. – Travis Griggs May 03 '16 at 22:17
  • @TravisGriggs I certainly would've done if MartinR hadn't already covered it in [his answer on the duplicate](http://stackoverflow.com/a/25391725/2976878). – Hamish May 03 '16 at 22:19
  • Doesn't compare() check for the same value (==) and not the same object (===)? – boidkan May 03 '16 at 22:24

0 Answers0