-3

I made a calculator and I'm trying to put a = on the top after I hit a operator button, but everytime I run it like that, it crashes.

var equal = Double("=")
var DisplayValue: Double? {
    get {
        return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
    }
    set {
        display.text = "\(newValue)" + equal userstyping = false
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    In terms of crashing, avoid use of `!` unless you know if cannot fail. E.g. `return display.text == nil ? nil : NSNumberFormatter().numberFromString(display.text!)?.doubleValue`. Re the initialization of `equal` and the subsequent use of it when you're setting `display.text`, neither of those make sense, so you have to tell us what you were trying to do there. – Rob Mar 21 '16 at 02:11

1 Answers1

1

I'm not sure what your trying to accomplish with the line:

var equal = Double("=")

But, as "=" can not be interpreted as a nil, it is equivalent in effect to:

var equal : Double? = nil

At that point I think you probably get a crash when you execute:

display.text = "\(newValue)" + equal userstyping = false

Although it's really impossible to say since that won't compile unless you really have:

display.text = "\(newValue)" + equal
userstyping = false

Even that won't actually compile since you can't add String and Double?

Either way I try to look at this, we don't have your actual code if it's compiling and failing at run time.

David Berry
  • 40,941
  • 12
  • 84
  • 95