2

I'm running this code

if(left.comboOptions.count != right.comboOptions.count) {
    for index in 0...left.comboOptions.count-1  {
        print(left.comboOptions[index].selectedComboOptionDishes == right.comboOptions[index].selectedComboOptionDishes)
        print(left.comboOptions[index].selectedComboOptionDishes)
        print("vs")
        print(right.comboOptions[index].selectedComboOptionDishes)
        print()
    }
}

And the output I got was

Comparing comboOptionDish
1x Black Beans Wrap(277) vs 1x Black Beans Wrap(277)
Both are equal

true
[1x Black Beans Wrap(277)]
vs
[1x Black Beans Wrap(277)]

false
[1x Fresh Lemon Juice(292)]
vs
[1x Fresh Lemon Juice(292)]

true
[]
vs
[]

Why is == on Set() returning false on index 1 (second one here)?? and Why is == block not executed for Fresh Lemon Juice??? What am I doing wrong? Or the way == operates on Set() differs specifically?

and my ComboOptionDish class is

class ComboOptionDish: Equatable, Hashable, CustomStringConvertible {

    var id: Int!
    var quantity: Int = 0
    var dish: Dish! /*Contains name, id and price and nothing else*/
    var description: String {
        return String(quantity) + "x " + dish.name + "(" + String(id) + ")"
    }

    var hashValue: Int {
        return self.id * self.quantity
    }
}

func == (left: ComboOptionDish, right: ComboOptionDish) -> Bool {
    print("Comparing comboOptionDish")
    print(String(left) + " vs " + String(right))
    if left.id != right.id { print("IDs not equal\n"); return false }
    if left.quantity != right.quantity { print("Quantity not equal\n"); return false }
    print("Both are equal")
    print()
    return true
}

My ComboOption class is

class ComboOption: Mappable, Equatable {

    var id: Int!
    var name: String!
    var selectedComboOptionDishes: Set<ComboOptionDish> = Set()
    var comboOptionDishes: ComboOptionDish = []

}

EDIT: How I fill data?

  • I fill in data using JSON using ObjectMapper.
  • I have many ComboOptions each containing many ComboOptionDishes.
  • Then I make user choose ComboOptionDishes, which I add to selectedComboOptionDishes which is a Set() of ComboOptionDishes inside the corresponding ComboOption
  • And then I now need to compare if two ComboOptions are equal, so I need to override == for ComboOption
Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102

1 Answers1

0

I can't recreate your problem but I believe it's a bug with the equality overload. There's a similar topic about it here.

The workaround is to have ComboOptionDish subclass NSObject and use isEqual .

class ComboOptionDish: NSObject {
    override var description: String {
        //...
    }

    override var hash: Int {
        //...
    }

    override func isEqual(object: AnyObject?) -> Bool {
        if let other = object as? ComboOptionDish {
            return self.id == other.id && self.quantity == other.quantity
        } else {
            return false
        }
    }
}
Community
  • 1
  • 1
Code
  • 6,041
  • 4
  • 35
  • 75