Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
Im trying to have an if statement to check if a double value in a string is larger than 10 but i keen on getting an error "Binary operator '>' cannot be applied to operands of type 'Double?' and 'Int'" anyone know what i'm doing wrong.

swift 4
Code:
if (Double(MyValue) > 10) {
                    // value is bigger than 10
                } else {
// do something else
}

i'm using a variable string because i need it to be a string so i can format it later on, i defined it as
Code:
var MyValue = ""
 
I think that Swift, as a language, is quite laughable at times, esp. coming from Java programming. And, you brought up one of the best examples of that.

And, while optionals have their benefits, they also make me wanna smash my computer against the top edges of a dumpster.

However, where Swift really kicks ass is in the department of closures ... oh yeah ! Java has them too, now, but ...

And, of course, the fact that Swift is less stringent in the declaration of types and those pesky semi-colons ... awesome !

:)
 
  • Like
Reactions: abcdefg12345
Oh wait, now that I look back on your code, I see that your Double(string) value evaluates to Double?, which is an optional value.

You don't need a new variable. You can just add "!" to your Double(string), and it will work.

I guess I was wrong about Swift being stupid in this case. But, I've encountered many cases of such compiler complaints (unable to compare two different numerical types).
 
I think a cleaner, more Swifty solution would be to store your MyValue as a double or int (which it seems to be). You can then use a NumberFormatter to handle the formatting.

Or you can use optional binding to unwrap the value. I would try one of these methods first before using a force unwrap
 
I think that Swift, as a language, is quite laughable at times, esp. coming from Java programming. And, you brought up one of the best examples of that.
That is quite clueless. The problem is that the OP doesn't know about a basic feature of Swift, which is optional values. The constructor Double(String) can obviously fail if the String doesn't contain a number. That's why Double (String) doesn't and cannot produce a result of type Double, but one of type Double? , that is an optional Double. And an optional Double cannot be compared with an Int.

Your "solution" is really, really awful. The ! means "I am sure this conversion will succeed. If it doesn't then please crash the app". let d = Double ("1234x")! will just crash. Intentionally. Because you didn't check for nil, and it was nil.

The correct way is checking for the error. For example

if let d = Double (MyValue), d > 10 {
// MyValue contained a number, and the number is > 10.
} else {
// MyValue didn't contain a number, or the number was <= 10.
}

If you think it is worthwhile to write code that is safe and actually works, then Swift is the right language.
 
That is quite clueless. The problem is that the OP doesn't know about a basic feature of Swift, which is optional values. The constructor Double(String) can obviously fail if the String doesn't contain a number. That's why Double (String) doesn't and cannot produce a result of type Double, but one of type Double? , that is an optional Double. And an optional Double cannot be compared with an Int.

Were you bullied at school ? You seem to have quite a low self-esteem because you couldn't respond without attacking me.

This will be my first and last communication to you. Suggestion to you - try communicating in a more civil way, and life will be great. Take care.
 
The correct way is checking for the error. For example

if let d = Double (MyValue), d > 10 {
// MyValue contained a number, and the number is > 10.
} else {
// MyValue didn't contain a number, or the number was <= 10.
}

This is ambiguous - what if the value being <= 10 should produce different results from the value being nil? To be fair, the OP's post is ambiguous as they were forcing the unwrap, meaning a nil value would crash. However in most business cases, a nil value would be handled differently (safely) from an insufficient value.

A much cleaner way would be:

Code:
// Handle your unwrapping here, and specify what to do in case of a nil
guard let d = Double(MyValue) else { return }

// Then implement your if statement
if d > 10 {
    // Do what you want if d > 10
} else {
    // Do what you want if d <= 10
}

Although, I really think that MyValue should be stored as a Double from the start, and a NumberFormatter implemented later to handle whatever formatting is needed. That has a much lower cognitive burden for whoever ends up reading the code.
 
  • Like
Reactions: atmenterprises
Were you bullied at school ? You seem to have quite a low self-esteem because you couldn't respond without attacking me.

This will be my first and last communication to you. Suggestion to you - try communicating in a more civil way, and life will be great. Take care.

Well, I didn't attack you. I attacked your post, which demonstrates that you know very little about Swift, and included one very dangerous recommendation.
 
I guard majority of times but if each of the functions in a class guard the same variable...might want to reconsider your initial interface.
 
Swift, is not sure your value is exactly int. There are two options to make sure.
1) "!" you can indicate that this value will be exact int by adding the mark.
Code:
if (Double(MyValue)! > 10) {
//
}
2) You can specify the type when defining the value.
Code:
let value: Int = Double(MyValue)!
if (value > 10) {
//
}
 
Swift, is not sure your value is exactly int. There are two options to make sure.
1) "!" you can indicate that this value will be exact int by adding the mark.
Code:
if (Double(MyValue)! > 10) {
//
}
2) You can specify the type when defining the value.
Code:
let value: Int = Double(MyValue)!
if (value > 10) {
//
}

I think your #2 example would fail because you're trying to assign a string cast to a double to an Int variable. Also, you're assuming MyValue is not nil, which could cause a runtime error should it be nil or non-numeric.

Your #1 example explanation isn't right either. An exclamation mark doesn't convert a value into an Int. It unwraps an optional variable's value. In your example, you'd end up with a Double value, not an Int, and only if MyValue wasn't nil or a non-numeric value.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.