Swift Struct doesn’t conform to protocol Equatable?

Swift 4.1 (and above) Updated answer: Starting from Swift 4.1, all you have to is to conform to the Equatable protocol without the need of implementing the == method. See: SE-0185 – Synthesizing Equatable and Hashable conformance. Example: struct MyStruct: Equatable { var id: Int var value: String } let obj1 = MyStruct(id: 101, value: … Read more

How can I compare CLLocationCoordinate2D

Either the generic approach for comparing two instances of any given struct type: memcmp(&cllc2d1, &second_cllc2d, sizeof(CLLocationCoordinate2D)) or cllc2d1.latitude == cllc2d2.latitude && cllc2d1.longitude == cllc2d2.longitude should work, if you really want to be sure they’re exactly equal. However, given that latitude and longitude are defined as doubles, you might really want to do a “close enough” … Read more

How to make a Swift enum with associated values equatable

SE-0185 Synthesizing Equatable and Hashable conformance has been implemented in Swift 4.1, so that it suffices do declare conformance to the protocol (if all members are Equatable): enum ViewModel: Equatable { case heading(String) case options(id: String, title: String, enabled: Bool) } For earlier Swift versions, a convenient way is to use that tuples can be … Read more

Swift Equatable on a protocol

If you directly implement Equatable on a protocol, it will not longer be usable as a type, which defeats the purpose of using a protocol. Even if you just implement == functions on protocols without Equatable conformance, results can be erroneous. See this post on my blog for a demonstration of these issues: Swift Protocols … Read more

tech