Here is my code, I am a beginner using swift and my code does not work, the application should take the value of 'ageInput' textField and multiply it by 7 then show the results inside resultLabel, I always get the error:
Cannot assign a value of type 'int' to a value of type 'String?'
class ViewController: UIViewController {
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var ageInput: UITextField!
@IBAction func findAge(sender: AnyObject) {
var catAge = ageInput.text.toInt() ?? 0
catAge = catAge * 7 ?? 0
resultLabel.text = catAge
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
Where did i go wrong?
Try:
resultLabel.text = String (catAge)
You can also do:
resultLabel.text = "\(catAge)"
here i have tray to solve your problem using example and i hope your issue will solved..
var stringNumber = "1234"
var numberFromString = stringNumber.toInt()
println(numberFromString)
Note toInt():
If the string represents an integer that fits into an Int, returns the corresponding integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With