Binary Operator '===' Cannot Be Applied to Operands of Type 'Any' and 'Uibarbuttonitem!'

Binary operator '===' cannot be applied to operands of type 'Any?' and 'UIBarButtonItem!'

As the error message is saying. In Swift 3, Objecitve-C id is imported as Any, and you cannot call any operations for Any including ===, without explicit cast.

Try this:

if sender as AnyObject? === saveButton {

(All the same for other sender comparison.)

And remember, in Swift 3, as AnyObject has become one of the most risky operations, you should not use as AnyObject in other cases.

Binary operator '==' cannot be applied to operands of type '[AnyHashable : Any]?' and 'String'

As the error clearly states info is a dictionary. You have to check if the dictionary contains the error key

if let error = info[PHImageErrorKey] as? String {
print(error)
// show an alert
} else {
thumbnail = result!
}

Binary operator '==' cannot be applied to operands of type 'UILabel?' and 'String'

You don't give the line number the error is on, but looking at the text it mentions operation == so I'm guessing it's one of these:

if hardness == "Soft"{

else if hardness == "Medium"{

"Soft" and "Medium" are the strings, so hardness must be a 'UILabel?. Those types can't be compared to each other. You must want the text displayed on the button? Looking at the UILabel docs, there is a text property so you probably want to change this line to grab the string representing the button's text:

let hardness = sender.titleLabel.text

Are you using dynamic buttons? It would be less error prone to just compare the sender with the button your are checking for. Comparing hard-coded strings to the text of the button can lead to run-time errors. Maybe you didn't get the case right, misspelled the text, or decided to localize later so the text may be different in a different language. These errors wouldn't be caught at compile-time.

binary operator '==' cannot be applied to operands of type 'String!' and '[String]' in Swift 2 Xcode 7

You can't compare string and array of strings, but you can use contains:

 if barcodeTypes.contains(metadataObj.type)

Binary operator '+' cannot be applied to operands of type '_' and 'String'

If you want to reduce strings the default value must be an empty string ""

let sum = names.reduce("") { return $0 + $1 }

Actually

let sum = names.joined() 

does the same.

Binary operator '==' cannot be applied to operands of type '(Any) - Int' and 'Int'

Functions should be like

func onboardingWillTransitonToIndex(_ index:Int)
func onboardingDidTransitonToIndex(_ index:Int)

in your current code index is of type (Any) -> Int which can't be compared with 1 or 2 ( which are of Int type )

binary operator '/' cannot be applied to two 'Double' operands

The error is a bit misleading.

In the first set of code, array2 is implicitly declared as an array of Int. So any attempt to assign a value to an index of array2 will require an Int value.

The problem is that Double(value) / 2.0 results in a Double value, not an Int. So the compiler is looking for a version of / that returns an Int. And that version expects two Int parameters. Since you are supplying two Double parameters, you get the error mentioned in your question.

The solution is to either cast the result to an Int or use two Int parameters to /.

var array2 = [8, 7, 19, 20]

for (index, value) in array2.enumerated() {
array2[index] = Int(Double(value) / 2.0) // cast to Int
}

or

var array2 = [8, 7, 19, 20]

for (index, value) in array2.enumerated() {
array2[index] = value / 2 // Use two Int
}

The result will be the same in this case. 8 will be replaced with 4. 7 will be replaced with 3, etc.

The second set of code works as-is because you declare the array to be filled with Double so everything matches up with the correct type.

Binary operator '===' cannot be applied to operands of type 'Self.Element' and 'AnyObject'

This is a bug SR-7275 (actually a regression). It should be already fixed in Xcode 9.3.1.

Just remove the Iterator from the where clause:

extension Sequence where Element: AnyObject {
func containsObjectIdentical(to object: AnyObject) -> Bool {
return contains { $0 === object }
}
}

While Self.Element and Self.Iterator.Element for Sequence are the same, it seems the compiler cannot see the transitive equality.

Also, you might consider making the method type safe and only compare with objects of type Element:

extension Sequence where Element: AnyObject {
func containsObjectIdentical(to object: Element) -> Bool {
return contains { $0 === object }
}
}

Math Calculation for ImplicitlyUnwrappedOptional Values

Add these custom operator overloads to your project:

public func + <T: FloatingPoint>(lhs: T!, rhs: T!) -> T!
{
if let lhs = lhs, let rhs = rhs
{
return lhs + rhs
}

return nil
}

public func - <T: FloatingPoint>(lhs: T!, rhs: T!) -> T!
{
if let lhs = lhs, let rhs = rhs
{
return lhs - rhs
}

return nil
}

public func * <T: FloatingPoint>(lhs: T!, rhs: T!) -> T!
{
if let lhs = lhs, let rhs = rhs
{
return lhs * rhs
}

return nil
}

public func / <T: FloatingPoint>(lhs: T!, rhs: T!) -> T!
{
if let lhs = lhs, let rhs = rhs
{
return lhs / rhs
}

return nil
}

It will allow for what you were attempting:

var double1 : Double! = 1.0
var double2 : Double! = 2.0
var double3 : Double! = 3.0

let result = double1 - double2 - double3 // -4

And, as a bonus, this will work with any arbitrary FloatingPoint type (including Float and Float80).

How do I fix error: Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

The code is outdated Swift 2 code.

Replace .KeyPathExpressionType with .keyPath and .ConstantValueExpressionType with .constantValue

The type is NSExpression.ExpressionType, please read the documentation



Related Topics



Leave a reply



Submit