Questions tagged [equatable]

The Swift equatable protocol can be used to have objects use the equality operators for comparison.

144 questions
0
votes
0 answers

How can I test a type (not an instance) for Equatable?

From another question here on SO about testing for protocol conformance, this is the solution... public class Foo { final public var items:[TItem] = [] { didSet{ // This is allowed if TItem.self is…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
0
votes
1 answer

Using equals on two protocol objects

Ideally I'd have the server implement the Equatable protocol but I ran into issues. Here's my code protocol Server { var ipAddress: String { get } // simplified for this question } func ==(lhs: T, rhs: T) -> Bool { return…
Yogurt
  • 2,913
  • 2
  • 32
  • 63
0
votes
1 answer

Is `UIStatusBarStyle` equatable?

I'm using a struct containing a property of type UIStatusBarStyle and like to use the "auto equatable feature" of Swift 4.1 for this struct. The documentation of UIStatusBarStyle shows it's an enum of type int, but doesn't say "conforms to…
user3532505
  • 429
  • 1
  • 7
  • 18
0
votes
1 answer

(Swift)How to get the index of a tuple element (Date, MyClass) from a tuple array [(Date, MyClass)]?

I have an array of type tuple (Date, MyOwnClass) and try to find the index of a specific tuple that equals to my target tuple from the tuple array. XCode is giving me error saying "Binary operator == can not..." when I tried to use ".indexOf({ $0 ==…
Nick Chang
  • 1
  • 1
  • 4
0
votes
1 answer

Swift | Set with NSObject

I'm trying to create a Set with custom objects. This is working, If I use a Set of my custom objects there is no duplicates : public class AttributesGroup: Hashable, Equatable, Comparable { open var id: Int! open var name: String! open…
Aximem
  • 714
  • 8
  • 27
0
votes
1 answer

How to create extension with Equatable to delete custom array element?

I have created custom array type. Now I want to have one more method in it, which can remove the element of the array by passing the actual element as an argument. I know this can be achieved if we add the extension of the custom array as…
Nick
  • 1,127
  • 2
  • 15
  • 40
0
votes
2 answers

Swift work around to make `AnyObject.Type` conform to `Equatable`?

I would like to make AnyClass (AKA AnyObject.Type) conform to Equatable so that when I have an array with AnyClass elements (Array), I can call remove(Element:), but this requires Element to conform to Equatable.
rolling_codes
  • 15,174
  • 22
  • 76
  • 112
0
votes
1 answer

Extension for sequences of dictionaries where the values are Equatable

I tried to implement the following method to remove double entries in an array of dictionaries by comparing their specific keys. However, this extension method will not work due to the error: Binary operator == cannot be applied to two 'Equatable'…
Suryu
  • 25
  • 1
  • 9
0
votes
0 answers

"Incomplete" Equatable implementation (Swift)

Is it a good idea to implement the Equatable protocol for a Swift struct in a way to compare only ID-s (which is supposed to be unique) and ignore the rest of the properties? What are drawbacks of this approach? struct Person { let id: String …
Artem Stepanenko
  • 3,423
  • 6
  • 29
  • 51
0
votes
1 answer

Hashable protocol for a protocol

I am wondering what is wrong with the following code ? import Foundation enum SliderType: Int { case analog = 1, discrete, highLow } protocol DataEntry: class, Hashable { var hashValue: Int { get set } // hashable protocol requires this …
iKK
  • 6,394
  • 10
  • 58
  • 131
0
votes
1 answer

Swift indexOf inconsistent behavior

It seems that I've facing inconsistent behavior with Swift's indexOf function. There are two indexOf functions in Swift: 1. The first one takes Equatable as an argument: array.indexOf(obj) 2. The second takes matching closure as an argument:…
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
0
votes
2 answers

Should CIImage be Equatable?

So, Apple’s documentation says that CIImage conforms to Equatable. I would take this to mean that the following unit test would pass. However, it doesn’t. I’m interested in why. func test_CIImageEqualityShouldWork() { let bundle =…
Luke
  • 9,512
  • 15
  • 82
  • 146
0
votes
2 answers

Swift - Struct with equatable variable

I'm trying to implement a struct which is Equatable and has a variable (in this example 'variable2') of type AnyObject that might or not be equatable. struct MyStruct : Equatable{ var variable1 : String; var variable2 : AnyObject; } func ==…
Guilherme
  • 503
  • 4
  • 15
0
votes
1 answer

Testing array containment for values defined as Any, but guaranteed to be of the same type.

Building on this question of mine (and the accepted answer), I want to test containment of a value in an array. The value is stored in a variable defined as of type Any, and the array is defined as [Any]. The actual types of the value stored in the…
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
0
votes
0 answers

Swift: '==' cannot be applied between two Equatable operands

I'm using Swift 1.2, and I'm having a hard time understanding why this extension does not compile. I must be missing something - T is Equatable and therefore I thought I should be able to compare via the '==' operator? extension Array { func…
1 2 3
9
10