4
class Person: Equatable {
var name: String
var age: Int

init(name: String, age: Int) {
    self.name = name
    self.age = age
}
static func ==(lhs: Person, rhs: Person) -> Bool {
    return (lhs.name == rhs.name) && (lhs.age == rhs.age)
}
}
let p1 = Person(name: "David", age: 29)
let p2 = Person (name: "John", age: 28)

var people = [Person]()
people.append(p1)
people.append(p2)

let p1index = people.index(of: p1)
let p2index = people.index(of: p2)

In order to use index(of:), i need to conform Person to Equatable protocol. From what i know, Equatable protocol allows 2 instances to be compared for equality. What is the relation between finding an index and compare for equality? Why I must conform to Equatable protocol in order to use index(of:)?

To adopt the Equatable protocol, I must implement the (==) operator as a static method:

static func ==(lhs: Person, rhs: Person) -> Bool {
    return (lhs.name == rhs.name) && (lhs.age == rhs.age)
}

I am not comparing any Person instances for equality here, why do I still need to return (lhs.name == rhs.name) && (lhs.age == rhs.age)? What is it for? Does it have anything to do with index(of:)?

Sorry for asking this stupid question.

Frankie
  • 185
  • 1
  • 1
  • 13
  • 1
    Does [the implementation of `index(of:)`](https://github.com/apple/swift/blob/master/stdlib/public/core/CollectionAlgorithms.swift.gyb#L64) answer your question? :) – Hamish Jun 11 '17 at 19:59
  • 1
    Although I'm not sure I understand the second part of your question, "*I am not comparing any Person instances for equality here*" – yes you are, you're comparing `lhs`'s properties with `rhs`'s properties. – Hamish Jun 11 '17 at 20:00
  • It helps. Thank you very much :) – Frankie Jun 11 '17 at 20:12

3 Answers3

4

Ask yourself this: what does index(of:) do exactly?

It finds the index of the specified element in the array. Right? In other words, it finds the index of the first element that is equal to the parameter.

A simple implementation for index(of:) would be to loop through the array and in each iteration, check whether the element is equal to the parameter. If it is, return the index. As you can see, this involves checking whether two objects are equal, so the Equatable protocol is required.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

indexOf(:) in this case, required to confirm Equatable protocol.

NOTE:

  • As indexOf(:) iterate array and compare your passed object to every element in the array which indirectly comparing and returning the index of matched object.

For eg:

let p1index = people.index(of: p1)

Here, p1index can find by comparing it to every element in the people array and return the index by comparing properties of p1 object match with properties of the object in the people array at that particular index.

Bhuvan Bhatt
  • 3,276
  • 2
  • 18
  • 23
1

you can use class Person: NSObject if you don't want to add additional protocol methods.

atline
  • 28,355
  • 16
  • 77
  • 113
Tang1230
  • 65
  • 1
  • 6