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

binary operator python
what is binary operator in c++ with example
binary operator c#
binary operator calculator
binary operator overloading in c++
what is binary operator in computer
binary operator vs unary operator
binary operator and unary operator

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

import UIKit

class ViewController: UIViewController {
  let Soft = 5
  let Medium = 8
    let Hard = 12

    @IBAction func hardnessSelected(_ sender: UIButton) {
        let hardness = sender.titleLabel

        if hardness == "Soft"{
        else if hardness == "Medium"{
            print (Medium)
        else {
            print (Hard)



How can i fix this error?

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 Operators, 6.5 Binary Operators � Multiplicative operators: multiplication (*), remainder (%), and division (/) (see Section 6.5. � Additive operators: addition (+) and subtraction (-)� In mathematics, a binary operation or dyadic operation is a calculation that combines two elements (called operands) to produce another element. More formally, a binary operation is an operation of arity two. More specifically, a binary operation on a set is an operation whose two domains and the codomain are the same set.

You're trying to compare two different types. To get the actual text of UILabel, you'll need hardness.text.

Binary operation, Binary logic operators take pairs of bits from each operand and compute the result according to the defined table of truth. Logic operators, on the other hand,� Redefinable Binary Operators To declare a binary operator function as a nonstatic member, you must declare it in the form: ret-type operator op (arg) where ret-type is the return type, op is one of the operators listed in the preceding table, and arg is an argument of any type.

UIButton.titleLabel is a UILabel and it stores its text in UILabel.text property:

let hardness = sender.titleLabel.text

In the case of UIButton you can also access UIButton.currentTitle property:

let hardness = sender.currentTitle

Bitwise operation, Binary operators are those operators that work with two operands. For example, a common binary expression would be a + b—the addition operator (+) surrounded by two operands. The binary operators are further subdivided into arithmetic, relational, logical, and assignment operators. The binary operators take two arguments and following are the examples of Binary operators. You use binary operators very frequently like addition (+) operator, subtraction (-) operator and division (/) operator. Following example explains how addition (+) operator can be overloaded.

An UIButton exposes its label through an UILabel that manage the drawing of its text. Thus change:

let hardness = sender.titleLabel


let hardness = sender.titleLabel.text

UIKit docs says:


var titleLabel: UILabel?

A view that displays the value of the currentTitle property for a button.



var text: String?

The current text that is displayed by the label.

There is also a more direct way using the currentTitle:


var currentTitle: String?

The current title that is displayed on the button.


let hardness = sender.currentTitle

will also work.

Binary Operator, Interface BinaryOperator<T>. Type Parameters: T - the type of the operands and result of the operator. In the C programming language, operations can be performed on a bit level using bitwise operators. Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR and NOT operators. Instead of performing on individual bits, byte-level operators perform on strings of eight bits at a time. The reason for this is that a byte is normally the smallest unit of addressable memory. This applies to bitwise operators as well, which mea

Binary Operators | Writing C# Expressions, Redefinable Binary Operators. Operator, Name. ,, Comma != Inequality. %, Modulus. %=, Modulus/assignment. &, Bitwise AND. &&, Logical� The following operators perform bitwise or shift operations with operands of the integral numeric types or the char type: Unary ~ (bitwise complement) operator Binary << (left shift) and >> (right shift) shift operators Binary & (logical AND), | (logical OR), and ^ (logical exclusive OR) operators

BinaryOperator (Java Platform SE 8 ), You use binary operators very frequently like addition (+) operator, subtraction (-) operator and division (/) operator. Following example explains how addition (+)� Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, which is 0011 0001. ~. Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = -61, which is 1100 0011 in 2's complement due to a signed binary number. <<.

Binary Operators, Binary Operator. An operator defined on a set S which takes two elements from S as inputs and returns a single element of S . Binary operators are called�

  • What do you think that error is trying to tell you?