Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot assign a value of type 'int' to a value of type 'String?'

Tags:

ios

xcode6

swift2

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?

like image 531
Bader H Al Rayyes Avatar asked Sep 02 '15 20:09

Bader H Al Rayyes


3 Answers

Try:

resultLabel.text = String (catAge)
like image 177
Andrey Avatar answered Nov 19 '22 10:11

Andrey


You can also do:

resultLabel.text = "\(catAge)"
like image 28
lucaslt89 Avatar answered Nov 19 '22 10:11

lucaslt89


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.
like image 2
Akash Raghani Avatar answered Nov 19 '22 09:11

Akash Raghani