Binary Operator '==' Cannot Be Applied to Operands of Type 'Uilabel' and 'String'

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 'UILabel?' and 'Double'

The error is pretty clear. numOfPrecentile is a UILabel?. You can't compare that to a Double. I would suggest storing your calculation into a Double first before setting numOfPercentile.text.

@objc func calculate() {
if let yourHeightTxtField = yourHeightTxtField.text, let yourWeightTxtField = yourWeightTxtField.text {

if let height = Double(yourHeightTxtField), let weight = Double(yourWeightTxtField) {
view.endEditing(true)
percentile.isHidden = false
numOfPercentile.isHidden = false
let bmi = BMI.getPercentile(forWeight: weight, andHeight: height)
numOfPercentile.text = "\(bmi)"
if bmi <= 18.5 {
percentile.text = "you are underweight"
}
}
}
}

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

Others have explained why your code gives an error. (String.randomElement() returns an Optional<String>, which is not the same thing as a String, and you can't use + to concatenate Optionals.)

Another point: Your code is verbose and repetitive. There is a principle, "DRY", in computer science. It stands for "Don't Repeat Yourself". Any time where you have the same code over and over (maybe with slight variations) it is "code smell", and an opportunity to improve.

You could rewrite your code without repetition like this:

func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

var result = ""
for _ in 1...6 {
result.append(alphabet.randomElement() ?? "")
}
print(result)
}

In that version, the expression alphabet.randomElement() only appears once. You don't repeat yourself. It also uses the ?? "nil coalescing operator" to convert possibly nil results to blank strings.


Another way to handle it would be to define an override of the += operator that lets you append String optionals to a string:

public func +=(left: inout String, right: Optional<String>) {
left = left + (right ?? "")
}

Then your function becomes:

func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var result = ""
for _ in 1...6 {
result += alphabet.randomElement()
}
print(result)
}

Yet another way would be to shuffle your source array, fetch the first 6 elements, and then join them back together into a String:

func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

let result = alphabet
.shuffled()[1...6]
.joined()
print(result)
}

Swift: Why ?? can not use for String?

String and UUID are not the same types, so they cannot be compared.

If you want to compare them, you could get the value of the UUID as a String using the UUID.uuidString property.

let x: String? = nil
let uuid = UUID()

let y: String = x ?? uuid.uuidString

How can I add some custom Text to my LocalizedStringKey?

This is how you add text together in swiftUI.

var body: some View {
Text(sometext) + Text("some more text")
}

cannot be applied to operands of type 'UITextField' and 'Int'

Your first line is calculating the doubleValue of what's entered into the text field, but you're not using that hours variable. Perhaps you want:

var hoursInAYear = hours * 365

The warning you are getting is telling you that you're trying to use the * operator between a variable whose type is UITextField and another variable whose type is Int (this is what your 365 literal is interpreted as).

This warning will come up any time we try to use an operator between two types for which the operator does not have an overload. It is particularly common when one of our operand's types is implicitly determined because we're using a literal somewhere. To resolve the issue, we must double check our instantiation of our operands and be sure they're of types for which our operator has an overload.

If they are not, then we should either change how we create these variables so they have the right type, or find some way of converting them when we use them with the operator.

When we change our mistaken variable from the text field to the double we just calculated, Swift is able to calculate this correctly. Despite previously claiming that 365 was an Int, being a literal, it can be interpreted as several different types, one of which includes Double.

When we attempt to use the * between a variable of type Double and a literal number, the literal number will be correctly converted to a Double, and we'll use the overload of the * operator which accepts two doubles (and returns a double).



Related Topics



Leave a reply



Submit