Swift Struct doesn't conform to protocol Equatable?
OK, after lots of searching, it's working...
struct MyStruct {
var id: Int
var value: String
init(id: Int, value: String) {
self.id = id
self.value = value
}
var description: String {
return "blablabla"
}
}
extension MyStruct: Equatable {}
func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
let areEqual = lhs.id == rhs.id &&
lhs.value == rhs.value
return areEqual
}
My Struct was in a class, so it didn't work.. I moved this Struct out of my class and now it's good :)
Swift - struct type does not conform to protocol
Store
has a property that is an array of Category
. I would guess that Category
does not conform to Equatable
or Hashable
, and so Swift cannot synthesize conformance.
Landmark
contains properties that all conform to Equatable
and Hashable
, so it can synthesize conformance for Landmark
.
There are two solutions to make Store
conform.
Make
Category
conform to both protocols. Then Swift could synthesize conformance forStore
.Explicitly implement the conformance for
Hashable
andEquatable
forStore
by implementingstatic func == (lhs: Store, rhs: Store) -> Bool
andfunc hash(into: inout Hasher)
Depending on the implementation of Category
, the simplest solution just might be:
extension Category: Hashable, Equatable { }
Struct will not conform with equatable, even with == set
Swift’s typesafety is very strict. Even if you implement == it doest count unless you state conformity to the Equateable protocol.
struct MenuItem: Equatable {
let image: UIImage
let title: String
let body: String
let storyboardName: String
let storyboardID: String
let landingItems: [LandingItem]
static func == (lhs: MenuItem, rhs: MenuItem) -> Bool {
return lhs.storyboardID == rhs.storyboardID
}
}
struct LandingItem: Equatable {
let image: UIImage
let title: String
let body: String
let storyboardName: String
let storyboardID: String
static func == (lhs: LandingItem, rhs: LandingItem) -> Bool {
return lhs.storyboardID == rhs.storyboardID
}
}
Value of protocol type 'Any' cannot conform to 'Equatable' Swift
As the error says, you cannot compare two dictionaries like that as they dont conform to Equatable
protocol. It will be better to use a struct for your data model instead of Dictionary
.
struct Library: Equatable {
let id: String
...
}
But if you don't want to do that, you can still check for equality with your dictionaries by equating the value of any keys in it.
var arr = libraryArray.filter { (dict) -> Bool in
dict["id"] as? String == songDict["id"] as? String
}
Swift enum conformance to Equatable when result type used as associated value: Type doesn't conform to protocol Equatable
Enum can automatically conform to Equatable
if its associated values conform to Equatable
, from docs:
For an enum, all its associated values must conform to Equatable. (An enum without associated values has Equatable conformance even without the declaration.)
And the Result<Success, Failure>
only conforms to Equatable
when
Success
conforms toEquatable
,Failure
conforms toEquatable
, andFailure
conforms toError
.
Your result's failure is only conform to Error
and Error
is not Equatable
yet. You can try to replace your Error
with a type that conforms to both Error
and Equatable
enum BookAction: Equatable {
case dataResponse(Result<Book, ActionError>)
}
struct ActionError: Error, Equatable { }
Ref:
https://developer.apple.com/documentation/swift/equatable
https://developer.apple.com/documentation/swift/result
Type '[String]' does not conform to protocol 'Equatable'
Array doesn't conform to Equatable
in Swift. Conforming to Equatable
implies there exists ==
operators for this type but not vice verse. Therefore even if you implement ==
for [String]
type, there's no way you can use Cell<[String]>
.
In your case, I would suggest using the following protocol:
public class Cell<T : SequenceType where T.Generator.Element: Equatable> {}
class TagsCell : Cell<[String]> {
}
Use a wrapper out of [String]
:
public class Cell<T : Equatable> {}
struct StringArray: Equatable {
var value: [String]
}
class TagsCell : Cell<StringArray> {
}
func ==(lhs: StringArray, rhs: StringArray) -> Bool {
return lhs.value == rhs.value
}
Error: not conform to protocol 'Equatable'
In Swift 3, operators required for protocols are declared as static members.
One way to work with such protocols is moving your operator definitions into the struct as static member:
struct SuitedCard: Comparable { //### Comparable implies Equatable
//...
// function for custom operator ==
static func ==(lhs: SuitedCard, rhs: SuitedCard) -> Bool {
//...
return false
}
// function for custom operator <
static func <(lhs: SuitedCard, rhs: SuitedCard) -> Bool {
//...
return false
}
}
Another way, you can declare the conformance to the protocol using extension:
struct SuitedCard {
//...
}
// function for custom operator ==
func ==(lhs: SuitedCard, rhs: SuitedCard) -> Bool {
//...
return false
}
// function for custom operator <
func <(lhs: SuitedCard, rhs: SuitedCard) -> Bool {
//...
return false
}
extension SuitedCard: Comparable {}
Automatic synthesis of Equatable conformance for Swift struct or enum through an extension
The proposal (SE-0185) for the synthesis says something different to the documentation you linked:
Users must opt-in to automatic synthesis by declaring their type as Equatable or Hashable without implementing any of their requirements. This conformance must be part of the original type declaration or in an extension in the same file (to ensure that private and fileprivate members can be accessed from the extension).
According to the proposal, declaring conformance in extensions in the same file also automatically generates the required members, which is in line with the actual behaviour. If you declare the extension in a different file from the type, you should see an error message:
Extension outside of file declaring struct 'Point' prevents automatic synthesis of '==' for protocol 'Equatable'
Related Topics
Coredata Crash Error Xcode 11 Beta, iOS 13 Beta
Create a Timer Publisher Using Swift Combine
Get Playground to Display All Loop Results
Building User Database Model in Firebase
Switch That Checks Nsindexpath's Row and Section
Swift: Force Show Navigation Bar in Modal
Can't Able to Get Video Tracks from Avurlasset for Hls Videos(.M3U8 Format) for Avplayer
Scenekit Object Between Two Points
Finding the Difference in Time Between Two Nsdates Swift
Change What Print(Object) Displays in Swift 2.0
Present Actionsheet in Swiftui on iPad
Unknown Selected Data Source for Port Speaker (Type: Speaker)
Using Foreach with a an Array of Bindings (Swiftui)
How to Override Layerclass in Swift
Local Swift Packages Stopped Working in Xcode 13