-1

This code worked fine in swift 1.0 except when I tried to run it with swift 2.0 it gave me an error..... Can someone explain why swift is giving me this error and tell me how to fix it? (I've already did some digging on stack overflow to try to find the solution but couldn't find it)

Thanks!! heres the code:

class ViewController: UIViewController {@IBOutlet var guess: UITextField!





@IBOutlet var label: UILabel!


@IBAction func guessButton(sender: AnyObject) {




    var randomnumber = arc4random_uniform(6)


    var guessInt = guess.text!.toInt()


    if guessInt != nil {


        if Int(randomnumber) == guessInt {

            label.text = "correct"


        } else {

            label.text = "nope it wsa \(randomnumber)"

        }





    } else {


        label.text = "enter a number"


    }


    print(guess.text)








}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

1 Answers1

0

I'm not sure what error message you're getting, but one thing I see wrong is the following:

var guessInt = guess.text!.toInt()

You should use Int(value) instead. You can also initialize your guessInt in an if string to check if the value is nil:

if let guessInt = Int(guess.text!) {

    //Do things

} else {

    //Value was nil

}
Setleaf
  • 75
  • 1
  • 7