-1

this is my line of code.

budgetLabel.text = String((budgetLabel.text)!.toInt()! - (budgetItemTextBox.text)!.toInt()!)

the code works, but when I try to input a floating value into the textbox the program crashes. I am assuming the strings need to be converted to a float/double data type. I keep getting errors when i try to do that.

asmcriminal
  • 93
  • 12

3 Answers3

2

In Swift 2 there are new failable initializers that allow you to do this in more safe way, the Double("") returns an optional in cases like passing in "abc" string the failable initializer will return nil, so then you can use optional-binding to handle it like in the following way:

let s1 = "4.55"
let s2 = "3.15"

if let n1 = Double(s1), let n2 = Double(s2) {
   let newString = String( n1 - n2)
   print(newString)
}
else {
  print("Some string is not a double value")
} 

If you're using a version of Swift < 2, then old way was:

var n1 = ("9.99" as NSString).doubleValue  // invalid returns 0, not an optional. (not recommended)

// invalid returns an optional value (recommended)
var pi = NSNumberFormatter().numberFromString("3.14")?.doubleValue
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
1

Fixed: Added Proper Handling for Optionals

let budgetLabel:UILabel = UILabel()
let budgetItemTextBox:UITextField = UITextField()
budgetLabel.text = ({
     var value = ""
     if let budgetString = budgetLabel.text, let budgetItemString = budgetItemTextBox.text
     {
          if let budgetValue = Float(budgetString), let budgetItemValue = Float(budgetItemString)
          {
               value = String(budgetValue - budgetItemValue)
          }
     }
     return value
})()
Joseph Afework
  • 269
  • 1
  • 4
  • Thank you, I tried that. it would give me a compiler error. "binary operator '-' cannot be applied to 2 float operands" – asmcriminal Dec 10 '15 at 00:02
  • Xcode always suggests using `!`. It is never ok, Xcode is always wrong – R Menke Dec 10 '15 at 00:04
  • @RMenke okay, what is "unwrapping" and what are my "optionals?" I am a C++ programmer, recently trying to learn swift. – asmcriminal Dec 10 '15 at 00:05
  • `optionals` are declared with either `!` or `?` behind the type. `var myOptional : String? = nil` Force unwrapping is using the variable with `!` which does not check for `nil` and crashes your app when so. Optional binding is using an `if let` to unwrap it. Google all those words. – R Menke Dec 10 '15 at 00:11
  • @RMenke okay thanks. I did come across optional and unwrapping, but I didn't fully understand it. You explained it better than my professor. I get it now. – asmcriminal Dec 10 '15 at 00:17
  • 1
    @RMenke fixed to not use forced unwrapping – Joseph Afework Dec 10 '15 at 01:12
  • okay i see it, now it's giving me an error "if let budgetValue = (budgetString), let budgetItemValue = Float(budgetItemString)" Cannot find an initializer for type float – asmcriminal Dec 10 '15 at 02:20
  • @asmcriminal are you using Swift 2 or 1.x? – Joseph Afework Dec 10 '15 at 05:40
  • @asmcriminal I believe `Float(string:)` initializer was added in Swift 2.0, for Swift 1.x see http://stackoverflow.com/questions/24085665/convert-string-to-float-in-apples-swift – Joseph Afework Dec 11 '15 at 06:20
  • @asmcriminal Stop using old versions of Swift! Migration only becomes more of a pain if you wait longer. Swift 1.2 is dead buried and forgotten – R Menke Dec 12 '15 at 15:40
  • @RMenke The issue is i have to update the entire OS to support the swift version I don't know much about macs. not sure if i have to reformat or how it all works. This semester is over, so I am done with swift for now. – asmcriminal Dec 16 '15 at 10:02
0

You need to be using if let. In swift 2.0 it would look something like this:

if let
    budgetString:String = budgetLabel.text,
    budgetItemString:String = budgetItemTextBox.text,
    budget:Double = Double(budgetString),
    budgetItem:Double = Double(budgetItemString) {
        budgetLabel.text = String(budget - budgetItem)
} else {
    // If a number was not found, what should it do here?
}
Gordon Tucker
  • 6,653
  • 3
  • 27
  • 41