2

im pretty new to swift, and im just trying to build something to test out the waters. this is in relation to a previous question i had. I am building some code to take user input from a UITextField object, and basically im trying to figure out how to convert an Int to a UInt32, but nothing ive searched on SO or otherwise has really helped.

here is my code //this is where i call the user input.

var rangeInput: Int? {
    get {
        return Int(rangeInputTextField?.text ?? "") 
    }

//this is my function to create a range, and call a random number out of that range

  let viewController =  ViewController()
var x = ViewController().rangeInput
let y = (Int?(x!)) 
var number = arc4random_uniform(Int(y!))//ERROR OCCURS HERE "Cannot convert value of type 'Int' to expected argument type 'UInt32'

//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 = Int(y!) + 1
        }
        let range = 1...high
       return range

    }
    //pick random number from that list

    let pickRandom = number
}

edit: Converted UInt, using answer in comments, but am having an issue with unwrapping optionals. am I doing something wrong with forcibly unwrapping optionals?

humans
  • 549
  • 8
  • 18
  • `UInt32(yourNumber)` – zc246 Mar 31 '16 at 15:50
  • @zcui93 where? do i change it to arc4random_uniform(UInt32(Int(y!))) or something? – humans Mar 31 '16 at 15:51
  • Hi @GabrielMSC, you could do something like this `let unsignedY = UInt32(x)` then `let usignedRandomNumber = arc4random_uniform(unsignedY)` and finally `let number = Int(unsignedRandomNumber)` – neteot Mar 31 '16 at 15:56
  • Hi @neteot that would be great, i tested it, and it still gives me an error finding nil, while unwrapping an optional value, so i am not sure if there is some code i might need to add or how to do that – humans Mar 31 '16 at 16:00
  • Possible duplicate of [Convert Int to UInt32 in Swift](http://stackoverflow.com/questions/25579962/convert-int-to-uint32-in-swift) – Pranav Wadhwa Mar 31 '16 at 16:05
  • @penatheboss i looked at that one, and yes it is similar, but its a totally differrent situation. ill edit the title, there is more than one issue im having – humans Mar 31 '16 at 16:06
  • The answer should still work in you scenario. Saying `UInt32(yourInteger)` should be fine. How is it a different situation? – Pranav Wadhwa Mar 31 '16 at 16:08
  • @GabrielMSC you can `guard` with an else statement or a `if let`. Something like this `if let saveInt = x as? Int { let unsignedY = UInt32(saveInt) usignedRandomNumber = arc4random_uniform(unsignedY) let number = Int(unsignedRandomNumber) } else { //can't do it }` – neteot Mar 31 '16 at 16:08
  • @neteot that looks like it would work, but with where im declaring it, is top level, and i cant put an if let statement on the top level, do i have to wrap that in a function? or – humans Mar 31 '16 at 16:12

2 Answers2

0
let viewController =  ViewController()
var x = ViewController().rangeInput
var number = arc4random_uniform(UInt32(x!)) // <----- UInt32 conversion

However, do recommend to check the user input x and/or after the UInt32 conversion, in case user inputs something not sensible, using guard or if let

zc246
  • 1,514
  • 16
  • 28
0

Initializers are called in swift by listing the data type (UInt32 in this case) followed by parenthesis containing the parameters. For most basic data types in Swift, there are no parameters for the initializers. So if you ever want to make an Int, Float, Double, UInt32, String etc., just do

UInt32(Value)!//UInt32 can be substituted for any of the above values

The "!" unwraps the optional, and will result in an error in runtime like "unexpectedly found nil when unwrapping optional value" if the value is not a valid form of the data type, if you want to do this safely, you can do this

if UInt32(Value) != nil {
    //Valid Value
}else {
    //Invalid Value
}

Also this is unrelated to the question, but your if let statement will always be true because you are overriding the value of the parameter "high" to a UInt32 of 0. I could be wrong though.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
C1FR1
  • 133
  • 1
  • 9