lundi 20 juin 2016

Swift - the text field returns 0 even though I inserted a different number

I'm new to Swift, and I'm trying to make a little app that, given two values (a,b) returns a random number between the two numbers (a < randomnumber < b)

The user has to write the values of a and b in two different text fields and then just press a button and that's it

The problem is: I told the NSLog to display the two values just before generating the random number because I noticed something wasn't working as it was supposed to, and the result is that apparently the second value I insert, whether it is a or b, for some (unknown) reason, equals 0 ! For example, if I write in the a-textfield the value 10 and then I insert the value 40 in the b-textfield, the NSLog displays a = 10, b = 0. If I insert the b value before, the NSLog displays b = 40, a = 0. These are the lines of code :

@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!

}


@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!

}


@IBAction func randomInBetweenAction(sender: AnyObject) {
    NSLog("a=%ld, b=%ld", a, b)
    randomInBetween()
    unHide()
    Label.text = "\(randomNumberInBetween)"

}

as you can see the value a is equal to the value that is inserted in the TextA field and the value b is equal to the value that is inserted in the TextB field

the randomInBetween() is the function that generates a random number between a and b, and it does work.. the problem is that one of the values (the second one I insert) is always 0 for some reason, even if I set it to a different number!

Any ideas on why this happens and how to work it out?




Aucun commentaire:

Enregistrer un commentaire