Binary operator '>' cannot be applied to operands of type 'Double?' and 'Int'

Discussion in 'Mac Programming' started by abcdefg12345, Oct 21, 2017.

  1. abcdefg12345 macrumors 6502

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    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 = ""
     
  2. abcdefg12345 thread starter macrumors 6502

    abcdefg12345

    Joined:
    Jul 10, 2013
    #2
    fixed it by adding another variable
    Code:
    let CheckValue = Double(MyValue)!
    if CheckValue > 10 {
    
    }
     
  3. 0002378 Suspended

    0002378

    Joined:
    May 28, 2017
    #3
    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 !

    :)
     
  4. 0002378 Suspended

    0002378

    Joined:
    May 28, 2017
    #4
    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).
     
  5. Greene macrumors regular

    Greene

    Joined:
    Jul 15, 2015
    Location:
    Fort Worth
    #5
    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
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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.
     
  7. 0002378 Suspended

    0002378

    Joined:
    May 28, 2017
    #7
    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.
     
  8. Greene macrumors regular

    Greene

    Joined:
    Jul 15, 2015
    Location:
    Fort Worth
    #8
    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.
     
  9. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    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.
     
  10. SAIRUS macrumors 6502a

    Joined:
    Aug 21, 2008
    #10
    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.
     
  11. ynstek macrumors newbie

    ynstek

    Joined:
    Mar 14, 2018
    #11
    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) {
    //
    }
     
  12. atmenterprises macrumors 6502

    Joined:
    Jan 28, 2006
    #12
    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.
     

Share This Page

11 October 21, 2017