The Swift equatable protocol can be used to have objects use the equality operators for comparison.
Questions tagged [equatable]
144 questions
76
votes
10 answers
Swift Equatable on a protocol
I don't think this can be done but I'll ask anyway. I have a protocol:
protocol X {}
And a class:
class Y:X {}
In the rest of my code I refer to everything using the protocol X. In that code I would like to be able to do something like:
let a:X =…

drekka
- 20,957
- 14
- 79
- 135
61
votes
6 answers
How to make a Swift enum with associated values equatable
I have an enum of associated values which I would like to make equatable for testing purposes, but do not know how this pattern would work with an enum case with more than one argument.
For example, summarised below I know the syntax for making…
user1162328
51
votes
13 answers
How can I compare CLLocationCoordinate2D
I need a way of comparing two CLLocationCoordinate2D's however when I tried using == it wouldn't work. Please can someone help me out with the best way of comparing them?
user843337
46
votes
3 answers
How do I create a Set of custom objects (Swift)?
For my iOS app I have a model something like
class Person {
var Id: Int
var Name: String
init(id: Int, name: String?) {
self.Id = id
self.Name = name ?? ""
}
}
Then later on in my ViewController when I load data…
user4591756
34
votes
2 answers
Swift Protocol Implements Equatable
I have the the below Protocol:
protocol Cacheable {
//....//
func identifier() -> String
}
Can I make Cacheable implements Equatable ?
when I do the following:
extension Cacheable: Equatable {}
func ==(lhs:Cacheable,rhs:Cacheable) -> Bool…

Bobj-C
- 5,276
- 9
- 47
- 83
30
votes
4 answers
Swift Struct doesn't conform to protocol Equatable?
How do I make a structure conform to protocol "Equatable"?
I'm using Xcode 7.3.1
struct MyStruct {
var id: Int
var value: String
init(id: Int, value: String) {
self.id = id
self.value = value
}
var description: String…

Insou
- 1,303
- 1
- 13
- 17
30
votes
2 answers
Overridden == function for Equatable type not called for custom class that subclasses NSCoding and NSObject
The FooBar class below has to override the == function of the Equatable type.
However, calling contains on an array of FooBar objects does not cause a breakpoint inside the custom == function to get invoked. Is it possible another == function is…

Crashalot
- 33,605
- 61
- 269
- 439
30
votes
5 answers
Why must a protocol operator be implemented as a global function?
I've seen the answer to this Swift Equatable Protocol question that mentions how the == method must be declared in the global scope.
If I don't adopt Equatable, I still could declare == to test for equality between two of my types.
// extension Foo:…
user4151918
29
votes
2 answers
Binary operator '==' cannot be applied to two operands
I have a class with the protocol Equatable. The class looks like this:
class Item: Equatable {
let item: [[Modifications: String]]
init(item: [[Modifications: String]]) {
self.item = item
}
}
func ==(lhs: Item, rhs: Item) ->…

Henny Lee
- 2,970
- 3
- 20
- 37
21
votes
3 answers
using Equatable class with flutter_bloc
Why do we need to use the Equatable class with flutter_bloc? Also, what do we use the props for? Below is sample code for making a state using the bloc pattern in Flutter.
abstract class LoginStates extends Equatable{}
class…

Osama Mohammed
- 2,433
- 13
- 29
- 61
16
votes
4 answers
How to properly implement the Equatable protocol in a class hierarchy?
I'm trying to implement the == operator (from Equatable) in a base class and its subclasses in Swift 3. All of the classes will only be used in Swift so I do not want to involve NSObject or the NSCopying protocol.
I started with a base class and a…

rmaddy
- 314,917
- 42
- 532
- 579
16
votes
3 answers
How does Dictionary use the Equatable protocol in Swift?
In order to solve this question, I have been playing around with a custom struct that implements the Hashable Protocol. I'm trying to see how many times the equivalency operator overload (==) gets called depending on if there is a hash collision or…

Suragch
- 484,302
- 314
- 1,365
- 1,393
14
votes
1 answer
Difference between using ObjectIdentifier() and '===' Operator
Let's say I'm implementing a root class in Swift, which I declare adopts the Equatable protocol (I want to be able to tell if an array of my type contains a given instance or not).
What is the difference -if any, in this specific case- between…

Nicolas Miari
- 16,006
- 8
- 81
- 189
13
votes
2 answers
Redundant conformance of 'Generic' to protocol 'Equatable' in Swift 2.2
I am Having an error in Equatable while compilation.
I wanted to add find or contains method in the list to check the value.
My Code Below
class Generic: NSObject, Equatable, NSCoding //Am Having an error - Redundant conformance of 'Generic' to…

dhaval shah
- 4,499
- 10
- 29
- 42
11
votes
1 answer
"contextual closure type expects 2 arguments" error when using reduce in Swift 4
The following code compiles in Swift 3
extension Array where Element: Equatable {
var removeDuplicate: [Element] {
return reduce([]){ $0.0.contains($0.1) ? $0.0 : $0.0 + [$0.1] }
}
}
but produces the error
error: contextual closure…

Senocico Stelian
- 1,378
- 15
- 23