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()
/
}
}
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.