0

I'm trying to create some code in Swift that will take a user input, from a UITextField object, create a range from that input from 1 to X(User inputted number), and then pick a random number out of that range.

I am calling my user inputted text like this

var rangeInput: UInt32 {
    get {
        return ((rangeInputTextField?.text ?? "") as? UInt32)! // ERROR OCCURS HERE
    }
}

and creating my list and calling the number this way.

let viewController =  ViewController()
var x = ViewController().rangeInput
let y = (UInt32?(x))
var number = arc4random_uniform(y!)

//MARK: Class for random number

struct RandomNumber {
    // numberRange to change the value for 1...X(user input)
    //creates the list to be picked from. (pickRandom)
   func numberRange(high:  UInt32) ->Range<UInt32>{

        if let high = UInt32?(0) {
            print("Invalid number")
        } else { 
            let high = UInt32(y!) + 1
        }

        let range = 1...high
        return range

    }

    let pickRandom = number
}

and back in my view controller, I am calling it inside the function of my button, like this.

 @IBAction func rollButton(sender: UIButton) {

     rolledNumber?.text = "\(RandomNumber().pickRandom)"

     if let resignFirstResponder: Bool = resignFirstResponder() {
         self.rangeInputTextField?.resignFirstResponder()     //Closes keyboard
     }
     resignFirstResponder()
}

override func viewDidLoad() {
    super.viewDidLoad()
}

Here is what confuses me, my code compiles and runs, when I get to the simulator, everything seems fine, I press my text field, and my number pad pulls up, and I can enter my number, but when I press the Roll button I have set up, it crashes. it gives me an error saying "Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)" and "fatal error: unexpectedly found nil while unwrapping an Optional value" in the cmd line

on this line

return ((rangeInputTextField?.text ?? "") as? UInt32)!
humans
  • 549
  • 8
  • 18
  • you can't cast a string to UInt32, it fails giving you a nil, but the you force unwrap and of course it crash – Andrea Mar 31 '16 at 14:11
  • @Andrea well then my entire code has a flaw, because ive been using UInt32 to pick the random number.. is there a way i can take user input as a UInt32 other than that? – humans Mar 31 '16 at 14:16
  • The right way would be create a UInt32 from a character. Something like that UInt32(string) – Andrea Mar 31 '16 at 14:20
  • Why don't you just use a regular int. I can't imagine the use putting in a number larger than 32 bits anyways – pbush25 Mar 31 '16 at 14:23
  • @pbush25 i tried that, but using arc4random and an int gives me an error saying "cannot convert value of type 'int' to expected argument type "UInt32" – humans Mar 31 '16 at 14:28
  • So then why not just convert the Int to Int32 in your function, but still cast it as an Int from the textField because there's no problems doing that – pbush25 Mar 31 '16 at 14:33

2 Answers2

0

First, you can't cast a String to a UInt32.

Secondly, it sounds like you definitely want to get a number back, so I wouldn't recommend the empty string after the ternary operator.

Try this:

return UInt32(rangeInputTextField?.text ?? "0")

You still need to pass in the 0 as a string because you can't use the ternary operator between two different types (String and Int), but using this you will always get back either the number that the user entered with the number pad or 0 (or whatever number you choose to return by default).

Wes
  • 1,032
  • 7
  • 11
0

I solved my issue, i actually ended up scrapping my entire code, and creating a new struct.

this is what worked for me:

struct RandomNumber {
func getRandomNumber(x: UInt32) -> String{
    return String(arc4random_uniform(x) + 1)
}

}

all I have to do is call on the struct from my view controller

humans
  • 549
  • 8
  • 18