Problem with Calculator example toInt()

hello people,


im just learning swift. Im traying to program a Calculator and ive a little problem.


by compiation


// toInt is unavailable: use Int() Initializer


/

/

/

/

/

/

/

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var Screen: UILabel!

var firstnumber = Int()

var secondnumber = Int()

var istTypingNumber = false

var result = Int()

var operation = ""




@IBAction func number(sender: AnyObject) {

var number = sender.currentTitle

if istTypingNumber == true{

Screen.text = Screen.text! + number!!

} else {

Screen.text = number;

}

istTypingNumber = true

}




@IBAction func operation(sender: AnyObject) {

istTypingNumber = false

firstnumber = Screen.text!.toInt()! // toInt is unavailable: use Int() Initializer ?????????

operation = sender.currentTitle!!

}




@IBAction func equal(sender: AnyObject) {

secondnumber = Screen.text!.toInt()! // toInt is unavailable: use Int() Initializer ????

if operation == "+"{

result = firstnumber + secondnumber

}else if operation == "-" {

result = firstnumber - secondnumber

} else if operation == "x"{

result = firstnumber * secondnumber

} else {

result = firstnumber / secondnumber

}

Screen.text = "\(result)"

}




@IBAction func clear(sender: AnyObject) {

firstnumber = 0

secondnumber = 0

istTypingNumber = false

result = 0

Screen.text = "\(result )"

}




override func viewDidLoad() {

super.viewDidLoad()

/

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}

Answered by OOPer in 69620022

Maybe you are watching a tutorial intended for Swift 1.2 (or older) and using Swift 2.

An old code something like:

someString.toInt()

should be rewritten as:

Int(someString)


So, one line of your code:

firstnumber = Screen.text!.toInt()!

should be:

firstnumber = Int(Screen.text!)!

You will see how you can rewrite another line.


But, if you continue the old outdated tutorial, you will face at many other problems. Swift 2 was such a big change, that many Swift 1.x code got outdated.

Accepted Answer

Maybe you are watching a tutorial intended for Swift 1.2 (or older) and using Swift 2.

An old code something like:

someString.toInt()

should be rewritten as:

Int(someString)


So, one line of your code:

firstnumber = Screen.text!.toInt()!

should be:

firstnumber = Int(Screen.text!)!

You will see how you can rewrite another line.


But, if you continue the old outdated tutorial, you will face at many other problems. Swift 2 was such a big change, that many Swift 1.x code got outdated.

Although that will result in compilation, I disagree on the "should", because initializers don't chain, leaving us with Objective-C-reminiscent options.


Until Swift is represented with more than text, I recommend this syntax, where the present emoji trails the type to which to convert. (The emoji doesn't currently render on this godforsaken forum; this should read as IntPRESENT, not IntSPACE)


extension String {
  var Int  : Int? {return Int(self)}
  func Int  (radix radix: Int) -> Int? {
    return Int(self, radix: radix)
  }
}

thank u! its working, but now i see, i cant wirte more tan one number, i can write 1, or 2, or.. but i cant wirte 12 or 222... why? 😟

As far as I see, your code has no flaws, when Int things are fixed.

Check:

- Each number button's action is connected to the number method.

- Each number button's labels is properly set: "0" .. "9", it cannot have spaces, nor any other characters than the digit.

thank u! i had some spaces! hehe!:P

othe question...

how can i do the memory functions? i mean, the ms,mr and mc? 🙂

thank u

You may need a place to memorize the Memory, maybe as an instance variable. After that, ... Not too complex. Start coding.

Problem with Calculator example toInt()
 
 
Q