Binary Operator '==' Cannot Be Applied to Two Operands

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

Update: SE-0143 Conditional conformances has been implemented in Swift 4.2.

As a consequence, your code does compile now. And if you define Item as a struct

struct Item: Equatable {
let item: [[Modifications: String]]

init(item: [[Modifications: String]]) {
self.item = item
}
}

then the compiler synthesizes the == operator automatically,
compare SE-0185 Synthesizing Equatable and Hashable conformance


(Pre Swift 4.1 answer:)

The problem is that even if == is defined for the dictionary type
[Modifications: String], that type does not conform to
Equatable. Therefore the array comparison operator

public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool

cannot be applied to [[Modifications: String]].

A possible concise implementation of == for Item would be

func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.item.count == rhs.item.count
&& !zip(lhs.item, rhs.item).contains {$0 != $1 }
}

Your code compiles for [[String: String]] – if the Foundation
framework is imported, as @user3441734 correctly said – because then [String: String] is automatically converted to NSDictionary which conforms to
Equatable. Here is a "proof" for that claim:

func foo<T : Equatable>(obj :[T]) {
print(obj.dynamicType)
}

// This does not compile:
foo( [[Modifications: String]]() )

// This compiles, and the output is "Array<NSDictionary>":
foo( [[String: String]]() )

How to resolve Binary operator '==' cannot be applied to two 'Foo' operands ?

Foo has no parent class that has any properties to perform comparison, so it has no idea how to compare itself.

Instead you need to adopt the Equatable protocol, which will then tell you to include the == operator, at which point you would define your action:

Your other option is to use ===, which will compare the references, so the only way this will pass is both items are pointing to the same instance.

class Foo : Equatable{

var randomeVar:String?

func saySomething(){
print("Something")
}

static func ==(lhs: Foo, rhs: Foo) -> Bool
{
//what makes us equal
}
}

Binary operator '~=' cannot be applied to two 'Optional Error ' operands

Here, the word error actually refers to the parameter error, rather than a new variable:

case .ExportError(error):

So you are essentially saying:

// code for illustration purposes only, not supposed to work
(error as? ClassError) == .some(.ExportError(error))

Note that the two occurrences of error refers to the same thing. That's probably not what you meant, but the compiler thinks that is what you meant, and tries to pattern match the associated value of ExportError against error using the ~= operator, as error here is considered an expression pattern

However, both operands are of type Optional<Error>. The ~= operator on Optional<Wrapped> requires that Wrapped conforms to Equatable, but Error does not conform to Equatable as it is a protocol. Hence the error message.

You probably meant to introduce a new identifier, so you should use a binding pattern, rather than just an expression pattern.

case .ExportError(let exportError):
print("Error :", exportError)

getting error: Binary operator '==' cannot be applied to two 'x' operands, how to remove certain number of elements in this array of objects

== is defined on Equatable, which your Cards type is not. You either need to make Cards conform to equatable, and decide which of your properties counts towards "equal" (The same type? the same income? both? What about the images?), or compare directly on the properties you do care about.

Binary operator '==' cannot be applied to two 'Equatable' operands... what?

Replace Element == Equatable with Element: Equatable.

Binary operator '==' cannot be applied to two operands in if statement

The message is quite clear

Operator function '==' requires that 'Landmark' conform to 'Equatable'

The struct Landmark must conform to Equatable protocol in order for it to know what equality means in reference to the Struct. Update your struct to

struct Landmark: Equatable {
let id: String
let name: String
let location: CLLocationCoordinate2D

static func == (lhs: Landmark, rhs: Landmark) -> Bool {
//Your equating logic here
}
}

Binary operator '+' cannot be applied to two 'T' operands

Swift doesn't know that the generic type T has a '+' operator. You can't use + on any type: e.g. on two view controllers + doesn't make too much sense

You can use protocol conformance to let swift know some things about your type!

I had a go in a playground and this is probably what you are looking for :)

protocol Addable {
func +(lhs: Self, rhs: Self) -> Self
}

func add<T: Addable>(num1: T, _ num2: T) -> T {
return num1 + num2
}

extension Int: Addable {}
extension Double: Addable {}
extension Float: Addable {}

add(3, 0.2)

Let me know if you need any of the concepts demonstrated here explained

Binary operator '*' cannot be applied to two 'Int?' operands

The problem are the pointless and wrong type annotations. Remove them! All values are non-optional (and constants)

func calculateIMC(){

let textHeight = height.text
let textWeight = weight.text
let intHeight = Int(textHeight!) ?? 0
let intWeight = Int(textWeight!) ?? 0

let calculateHeight = intHeight * intHeight // probably intHeight * intWeight
}

Swift: binary operator ' ' cannot be applied to two 'Comparable' operands

The < operator applies to two values of a single type that conforms to Comparable — not to two things designated merely as Comparable. A protocol does not conform to itself.

The use of any with MyElement doesn't affect this basic fact about the Comparable-constrained generic placeholder T. Since MyElement is generic over T, we would have to know that the two different MyElement-conforming objects you propose to sort resolve their T to the same type as one another — and in the code you wrote, we don't know that, so the compiler balks.



Related Topics



Leave a reply



Submit