Swift Struct Doesn't Conform to Protocol Equatable

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.

  1. Make Category conform to both protocols. Then Swift could synthesize conformance for Store.

  2. Explicitly implement the conformance for Hashable and Equatable for Store by implementing static func == (lhs: Store, rhs: Store) -> Bool and func 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 to Equatable, Failure conforms to Equatable, and Failure conforms to Error.

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



Leave a reply



Submit