The Swift equatable protocol can be used to have objects use the equality operators for comparison.
Questions tagged [equatable]
144 questions
1
vote
0 answers
Flutter Equatable for partially immutable class
Dart says to avoid equality on mutable classes, that's reasonable. But what about classes with an immutable part like an unique id on which hashcode and equality shall be calculated with and other mutable attributes which shall be ignored. Is that…

Matthias
- 31
- 2
1
vote
0 answers
How do I solve this mixed enum equatable thing?
I have this
enum MyEnum {
case one
case two
}
and this
var array:[MyEnum] = [.one]
and later
func add(_ case:MyEnum) {
if array.contains(case) { return }
array.append(case)
}
So far so good.
Then I have added a third option to the…

Ronnie
- 332
- 2
- 11
1
vote
1 answer
What am I doing wrong with Equatable and BloC?
I have implemented a series of BLoCs for a flutter app and part of it works. The app is pretty simple and it tracks the state of a battle between two players with updates to the game state happening when the players press buttons. There is other…

user2939408
- 153
- 7
1
vote
1 answer
flutter: compare two object and got ERROR: Expected: VenuesDetails: in unit test
I am using Equatable to compare 2 object in unit testing.This is my object that extended from Equatable:
import 'dart:convert';
import 'package:equatable/equatable.dart';
class VenuesDetails extends Equatable {
Response response;
…

Cyrus the Great
- 5,145
- 5
- 68
- 149
1
vote
3 answers
How to compare custom objects based on different properties using Equatable?
I am using the equatable protocol in order to compare two custom objects based on one property named mediaUID.
Is there a way to switch between comparing on different properties?
In func fetchNotificationsRemovedsometimes I need to compare by …

bibscy
- 2,598
- 4
- 34
- 82
1
vote
1 answer
Hashable == method does not detect difference between two objects swift
I implemented the class below:
class Table : Hashable {
var uid : Int
var timeRemaining : Int?
var currentPrice : Double?
var hashValue: Int {
return uid.hashValue
}
static func ==(lhs: Table, rhs: Table) ->…

Alk
- 5,215
- 8
- 47
- 116
1
vote
1 answer
Swift: array of generics with types conforming Equatable
In Swift, how do you define an array of generics with type conforming Equatable?
Example:
struct File {
public var lines: [T]
private var lineCursor = 0
public var currentLine: T {
get { return lines[lineCursor] }
…

Rudy Phillipps
- 11
- 3
1
vote
2 answers
Remove duplicates from a multi-dimensional array
I wrote the following extension to remove duplicates from my Array.
extension Array where Element : Equatable{
func removeDups() -> [Element]{
var result = [Element]()
for element in self{
if…

mfaani
- 33,269
- 19
- 164
- 293
1
vote
2 answers
Binary operator '==' cannot be applied to two 'Equatable' operands... what?
You can see in the image below that I was trying to extend the Collection protocol to include a method called removingDuplicates, which is supposed to do exactly what it says. The error that the compiler is displaying seems to directly contradict…

jeremyabannister
- 3,796
- 3
- 16
- 25
1
vote
2 answers
why swift compiler behaves differently with equality operator with/without Equatable protocol
I have a very simple class in a Playground in Swift 4.0 that overrides the == operator.
I'm not understanding why the Swift complier doesn't behave the same when the class inherits/doesn't inherit Equatable protocol.
Here the class when inheriting…

sebastien
- 2,489
- 5
- 26
- 47
1
vote
1 answer
Swift Generic array Equatable casting
I have a class AAA which contains an generic array.
Since Item could be not Equatable, so I do not code it as class AAA.
I would like to add an remove function in class AAA which is available when Item is Equatable. By calling this…

Wilfred
- 187
- 3
- 15
1
vote
2 answers
Check if Swift array contains instance of object
How does one check if a Swift array contains a particular instance of an object? Consider this simple example:
class Car {}
let mazda = Car()
let toyata = Car()
let myCars = [mazda, toyata]
myCars.contains(mazda) // ERROR!
My investigations have…

Michael Knudsen
- 629
- 1
- 7
- 23
1
vote
0 answers
How to make a recursive equatable protocol for swift struct
Is there any way that can I set up a protocol such that if we define a struct where all its properties conforms to Equatable, then it becomes automatically Equatable without having to write all the boilerplate code to compare every properties within…

user2573217
- 41
- 4
1
vote
0 answers
Testing NSImages for equality
I have multiple identically-configured NSImageView objects in an NSPanel, all with drag-and-drop mode enabled. I’d like to cross-check the contained images to flag duplicates. But when I drop a single small gif or jpeg onto two of the controls, the…

Jeff J
- 139
- 8
1
vote
1 answer
Swift 2 Generic data structure not conforming to Equatable protocol
I am working on building a flexible data structure in Swift called Node that, by itself, is not tied to any type of content. However, the Payload data within the Node is declared as the following generic Element struct that conforms to the…

Alex Ackerman
- 23
- 5