
Robert C. answered 10/03/21
Senior iOS/Swift Developer, Lecturer, Computer Science Graduate
Lets give this a shot
Here we are just iterating over the array until we find a match. However, the compiler gives you an error stating:
Binary operator '==' cannot be applied to two 'Self.Element' operands
We can resolve that by enforcing conditional conformance to the Equatable protocol for Collection.Element.
Now let's try with a custom type:
The compiler gives us an error stating:
Referencing instance method 'firstIndex(of:)' on 'Collection' requires that 'Person' conform to 'Equatable'
There, that solves it.
Since person is a value type the compiler implements the Equatable methods for us since all of its properties conform to Equatable (String). Now let's try with a class.
The compiler gives us another error:
Type 'Person' does not conform to protocol 'Equatable'
This is because the compiler doesn't synthesize the methods for us when it comes to classes. So let's conform to Equatable manually.
And there you have it.