The Swift equatable protocol can be used to have objects use the equality operators for comparison.
Questions tagged [equatable]
144 questions
4
votes
1 answer
Swift 2.2, Contains Method not working
Contains method not working properly and it is giving me false result even if it is matching with Object?
My Code Below
class Generic: NSObject, NSCoding
{
var genericCode: String?
var genericName : String?
var genericType : String?
var…

dhaval shah
- 4,499
- 10
- 29
- 42
4
votes
3 answers
How to compare Equatable in Swift
I have multiple sets of two arrays like that. I am getting them from a 3rd party.
var array1 : [Any?]
var array2 : [Any?]
I know about types of objects in these array (in compile time). As example that first element will be a String and second is…

Victor Ronin
- 22,758
- 18
- 92
- 184
4
votes
2 answers
swift indexOf for [AnyObject] array
Trying to get the index of an array ([AnyObject]). What's the part that I'm missing?
extension PageViewController : UIPageViewControllerDelegate {
func pageViewController(pageViewController: UIPageViewController,…

el.severo
- 2,202
- 6
- 31
- 62
3
votes
2 answers
Property change from Int to String provokes SwiftUI view not updating
I have a strange behavior in my code. It's a simple app that shows four views (CardView) and each time one of them is tapped, the view will reflect it by changing its border (using "selected" property). The problem is that when "value" property is a…

rai212
- 711
- 1
- 6
- 20
3
votes
1 answer
List of equatable objects as member of an equatable object
I am currently following resocoders clean architecture in which he highly recommends to use Equatable for basic dataholding classes that will be compared in the future. The thing is my class will look something like this:
class Person extends…

jaaq
- 1,188
- 11
- 29
3
votes
1 answer
swift: why need to conform equatable when overload the "==" operator?
I'm learning swift and read a topic about operator overloading in extensions, which likes:
extension StreetAddress: Equatable {
static func == (lhs: StreetAddress, rhs: StreetAddress) -> Bool {
return
lhs.number ==…

Antonio Steve
- 33
- 1
- 4
3
votes
1 answer
Common Equatable class on Swift
I need container for Any Equatable items in NOT Generic class (for example UI classes initial from storyboard). I need like this
var items: [Equatable]?
but it don't work, Equatable need Generic. the problem that there is no exist common Equatable…

EvGeniy Ilyin
- 1,817
- 1
- 21
- 38
3
votes
2 answers
Swift Equatable Generic type comparison inside generic function
I have a Node class for a binary tree like so:
class Node {
let value: T
let left: Node?
let right: Node?
init(value: T, left: Node? = nil, right: Node? = nil) {
self.value = value
self.left…

richy
- 2,716
- 1
- 33
- 42
3
votes
2 answers
Swift 3: How to write Equatable func
I am trying to use struct as my key for dictionary.
Code works for swift 2, however not for swift 3 as in the picture link.
Equatable Code:

selcuk
- 73
- 1
- 10
3
votes
1 answer
Swift enums with arguments: how to compare them?
I have the following enum:
enum Message: ErrorType {
case MessageWithInfo(info:String?)
case MessageDidFail
case MessageDidSend(info:String)
case InvalidMessageData
case MessageWithDelay(delay:Double)
.... will keep adding…

zumzum
- 17,984
- 26
- 111
- 172
2
votes
1 answer
Why am I getting the Equatable error: The element type can't be assigned to the list type 'Object'
I upgraded Flutter to v 3.3.4 and am trying to fix the null safety issues with my blocs, but have run into a problem with Equatable props. When I make one of the event properties nullable, the equatable get props shows the following error on the…

user3735816
- 225
- 2
- 11
2
votes
1 answer
Swift subclass conform to equatable called super class implemenation?
class Base: Equatable {
static func == (lhs: Base, rhs: Base) -> Bool {
lhs.id == rhs.id
}
let id: String
init(id: String) {
self.id = id
}
}
class SubClass: Base {
public var id2: String?
public…

Johnny
- 2,589
- 2
- 27
- 34
2
votes
1 answer
XCTAssertEqual gives me error - UIViewController.Type cannot conform to 'Equatable'
I have an enum like following-
enum Vehicle: String, CaseIterable {
case car = "/car/"
case boat = "/plane"
case bicycle = "/bicycle/"
case train = "/train"
var targetControllerType:…

Natasha
- 6,651
- 3
- 36
- 58
2
votes
2 answers
How to pass an enum case into a function that uses 'if case' to check for a property's case
I have an enum where each case has different (or none) associated values. The enum is not equatable.
I'm using on several places if case to check if a value is of a specific case.
enum MyEnum {
case justACase
case numberCase(Int)
case…

Marco Boerner
- 1,243
- 1
- 11
- 34
2
votes
1 answer
Comparing Two Protocol Instances for Equality in Swift
Here's the deal,
I'm writing an SDK, and I want to declare observers as protocols, instead of classes or structs (It's sort of an "Observer/Delegate" hybrid).
I want to be able to compare two arguments that are passed in as protocol references, as…

Chris Marshall
- 4,910
- 8
- 47
- 72