0

I've tried implementing the dot or "point" in calculator I'm programming the windows 7 calculator as a reference to my calcu.

I've tried this in my code:

@IBAction func btnDot(sender: AnyObject) {
    if(lblResult.text!.characters.count == 0) {
        lblResult.text = "0."
    }
    else {
        if(Float(lblResult.text!.lowercaseString.characters.contains(".")) == -1) {
            lblResult.text = lblResult.text! + "."
        }
    }
    lblResult.text = lblResult.text
}

but the function btnDot doesn't seem to work. What seems to be the problem here?

Note: The lblResult.text is the Id of my UILabel for the display of results

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
laurence keith albano
  • 1,409
  • 4
  • 27
  • 59
  • *"doesn't seem to work"* is a useless problem description. What is the input, the actual result and the expected result? – And split your expression in smaller separate statements to isolate the problem (in other words: learn how to debug). – Martin R Dec 11 '15 at 18:03
  • *Hint:* `Float(someText.contains(".")) == -1` will never be true. Where did you find that? – Martin R Dec 11 '15 at 18:04
  • And this line: lblResult.text = lblResult.text does nothing – Simon Meyer Dec 11 '15 at 18:07
  • As I said I'm new to swift programming, I've only convert the code above base on my javascript code. It's my first attempt though, don't be strong @Martin R – laurence keith albano Dec 11 '15 at 18:16

1 Answers1

0

Try this code

if (lblResult.text?.characters.count == 0){
    lblResult.text = "0."
}
else{
    if lblResult.text!.rangeOfString(".") == nil{
       lblResult.text = lblResult.text! + "."
    }
}

If you want help you with your code you'll need to provide more info about the problem, now I just could give you a working example.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • I agree, but as I have updated the answer the user did not provide much information. @rmaddy – Rashwan L Dec 11 '15 at 18:16
  • Sorry about that, next time I'll improve my question. This code works, I'm just new to the syntax of xcode here. That's why I'm having a hard time learning its syntax. – laurence keith albano Dec 11 '15 at 18:19