3

I am trying to define Equatable using Swift structs. I am getting an error on the line func == saying Operators are only allowed at global scope.

struct ShoppingList {

    var shoppingListId :NSNumber
    var title :String

    init(title :String) {

        self.title = title
        self.shoppingListId = NSNumber(integer: 0)
    }
}

extension ShoppingList {

    public func ==(lhs :ShoppingList, rhs :ShoppingList) -> Bool {
        return lhs.title == rhs.title
    }

}

What am I missing?

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
john doe
  • 9,220
  • 23
  • 91
  • 167

1 Answers1

3

Exactly what it says. Move the operator function definition outside of the extension. It's done at a global scope.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610