equatable

How to make a Swift enum with associated values equatable

泄露秘密 提交于 2021-02-15 09:12:27
问题 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 heading equatable. How would this work for options, which contains multiple values of different types? enum ViewModel { case heading(String) case options(id: String, title: String, enabled: Bool) } func ==(lhs: ViewModel, rhs: ViewModel) -> Bool {

How to make a Swift enum with associated values equatable

浪尽此生 提交于 2021-02-15 09:11:11
问题 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 heading equatable. How would this work for options, which contains multiple values of different types? enum ViewModel { case heading(String) case options(id: String, title: String, enabled: Bool) } func ==(lhs: ViewModel, rhs: ViewModel) -> Bool {

Binary operator '==' cannot be applied to two operands

微笑、不失礼 提交于 2021-02-06 14:59:17
问题 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) -> Bool { return lhs.item == rhs.item } But this is giving me the error (see title). The property item was [[String: String]] before and there was no problem and I have no idea how to fix this. I tried googling and searching all over SO but with no luck.. The enum is just a

Swift Struct doesn't conform to protocol Equatable?

孤者浪人 提交于 2021-02-06 09:45:06
问题 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 { return "blablabla" } } When I use "MyStruct", Xcode shows the error: MyStruct does not conform to protocol "Equatable" Do you have an idea to make MyStruct conform to protocol? 回答1: Swift 4.1 (and above) Updated answer: Starting from Swift 4.1, all you have to is to conform to

Swift Equatable on a protocol

℡╲_俬逩灬. 提交于 2020-06-24 03:04:36
问题 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 = ... let b:X = ... if a == b {...} The problem is that if I try to implement Equatable : protocol X: Equatable {} func ==(lhs:X, hrs:X) -> Bool { if let l = lhs as? Y, let r = hrs as? Y { return l.something == r.something } return false } The idea to

App Crashing with error: generic parameter 'T' could not be inferred

旧巷老猫 提交于 2020-04-18 05:45:40
问题 I'm trying to get custom object which is hashable from UserDefault. My custom model is defined below: class WorkerProfileResponse: Mappable, Hashable{ static func == (lhs: WorkerProfileResponse, rhs: WorkerProfileResponse) -> Bool { return lhs.id == rhs.id } var hashValue: Int{ return self.id! } var id, loginStatus, lastLogin, lastActive: Int? var username, email, mobileNumber: String? var userCategories: [String]? var userSubCategories: [String]? var biometricToken: String? var accessToken:

Why must a protocol operator be implemented as a global function?

杀马特。学长 韩版系。学妹 提交于 2019-12-28 05:49:11
问题 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: Equatable {} func ==(lhs: Foo, rhs: Foo) -> Bool { return lhs.bar == rhs.bar } struct Foo { let bar:Int } The fact that its implementation needs to be declared at a global scope, makes it seem incidental to and distinct from a protocol, even if

Swift: Hashable struct with dictionary property

喜你入骨 提交于 2019-12-12 08:06:57
问题 I have a struct in Swift that looks like this: internal struct MapKey { internal let id: String internal let values: [String:String] } extension MapKey: Equatable {} func ==(lhs: MapKey, rhs: MapKey) -> Bool { return lhs.id == rhs.id && lhs.values == rhs.values } I now have the need to use MapKey as the key in a Swift dictionary, which requires MapKey to conform to the Hashable protocol. What would a correct implementation of Hashable be for a struct like this one? extension MapKey: Hashable

why swift compiler behaves differently with equality operator with/without Equatable protocol

痴心易碎 提交于 2019-12-11 06:54:27
问题 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 Equatable protocol class Test: Equatable { var value = 0 init(_ initialValue:Int) { value = initialValue } static func == (lhs:Test, rhs:Test) -> Bool { return lhs.value == rhs.value ? true : false } } let test1 = Test(0) var test4:Test? = nil if

Avoid Equatable and Hashable boilerplate, Swift 4.2

 ̄綄美尐妖づ 提交于 2019-12-11 04:25:33
问题 On project we are using classes for model's layer and because of that I have to write code like this: // MARK: - Hashable extension Player: Hashable { static func == (lhs: Player, rhs: Player) -> Bool { return lhs.hashValue == rhs.hashValue } func hash(into hasher: inout Hasher) { hasher.combine(self.name) } } Can this boilerplate can be somehow avoided? Is it possible to implement that Equatable compare by .hashValue by default? Thanks. 回答1: You could write your custom template via Stencil