I'm trying to learn swift by myself so sorry if this turns out to be very simple.
I want to make a function that, given two numbers (example: 5, 60) gives back random numbers between those two numbers (example: 33,56,12, but not 2 or 90).
I tried using this line of code on the Playground and it worked
var a : UInt32
var b : UInt32
a = 5
b = 60
let randomNumber = arc4random_uniform(b-a) + a
//gives back a random number between 5 and 60
However, when I create - using the same method - the function randomNumberInBetween in my viewController.swift, it gives me an error.
var a = UInt32()
var b = UInt32()
var randomNumberInBetween = Int()
func randomInBetween() {
randomNumberInBetween = Int(arc4random_uniform(b - a) + a)
// here I get this error: Thread 1 : EXC_BAD_INSTRUCTION (code=EXC_l386_INVOP,subcode=0x0)
}
@IBAction func TextAAction(sender: AnyObject) {
a = UInt32(TextA.text!)!
}
@IBAction func TextBAction(sender: AnyObject) {
b = UInt32(TextB.text!)!
}
@IBAction func randomInBetweenAction(sender: AnyObject) {
randomInBetween()
unHide()
Label.text = "\(randomInBetween)"
NSLog("\(randomInBetween)")
}
(PS: TextA is a Text field in which the user inserts the value a (ex: 5) TextB is a Text field in which the user inserts the value b (ex: 60) )
I've been trying to work it out by myself but I just lost hope, anyone knows what the problem is? Thank you:)
Aucun commentaire:
Enregistrer un commentaire