Swift Error: Binary Operator '&&' Cannot Be Applied to Two 'Bool' Operands

Binary operator '&=' cannot be applied to two 'Bool' operands in Swift

&= is a bitwise operation and can only be applied to integer types. &&= which is what you need doesn't actually exist because of short circuiting. You need to write

validParams = validParams && ...

Binary operator '&&' cannot be applied to two Bool operands

Here is some code that should do the trick. Remember to preceed throw statements with try and catch them.

func saveContext () {
if let moc = self.managedObjectContext {
if moc.hasChanges {
do {
try moc.save()
} catch {
NSLog("Unresolved error \(error)")
abort()
}
}
}
}

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)

Binary operator '&&' cannot be applied to two 'Int' operands

In Swift you cannot check for non-null like in C and Objective-C, you have to write

if firstName.length > 0 && lastName.length > 0 { }

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]]() )

Error comparision - Binary operator == cannot be applied two Error operands

You need to cast the Swift.Error to your custom ServiceError. The common approach is to use a switch:

enum ServiceError: Error {
case serverDown, userNotExist
}


let error: Error? = ServiceError.serverDown as Error

switch error as? ServiceError {
case .serverDown: print("Server is down") // "Server is down\n"
case .userNotExist: print("User not found")
case .none: print("error:", error ?? "nil")
}

Note: It is Swift naming convention to name your enumeration cases starting with a lowercase letter.



Related Topics



Leave a reply



Submit