tommsyeah tommsyeah - 5 months ago 16
iOS Question

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?

@ReinerMelian this is the full code

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var Label: UILabel!
@IBOutlet weak var Button: UIButton!
@IBOutlet weak var InvariableLabel: UILabel!
@IBOutlet weak var TextA: UITextField!
@IBOutlet weak var TextB: UITextField!
@IBOutlet weak var AndLabel: UILabel!
@IBOutlet weak var Button2: UIButton!

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

Hide()

}

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

var randomNumber = Int()


func randomNUMBER() -> Int {
randomNumber = Int(arc4random())
return randomNumber
}

func Hide() {
Label.hidden = true
}

func unHide() {
Label.hidden = false
}

var a = UInt32()
var b = UInt32()

var randomNumberInBetween = Int()

func randomInBetween() -> Int {
if b>a {
randomNumberInBetween = Int(arc4random_uniform(b - a) + a)
return randomNumberInBetween

} else {
randomNumberInBetween = Int(arc4random_uniform(a - b) + a)
return randomNumberInBetween
}


}



@IBAction func ButtonAction(sender: AnyObject) {
randomNUMBER()
unHide()
Label.text = "\(randomNumber)"
NSLog("\(randomNumber)")
}


@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()
NSLog("\(randomInBetween)")
unHide()
Label.text = "\(randomNumberInBetween)"
NSLog("\(randomInBetween)")
}



}


(PS: as you can see, there is also a function that generates a random number without any sort of constraints, but that part works just fine.. the problem is with the other part of the code, the one that returns a random number between two given values (a,b) )

Answer

make sure, your outlets (TextA, TextB) and your IBAction (randomInBetweenAction) are wired up correctly, remove

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

}

and

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

}

and try this function on button tap:

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