Dakata Dakata - 2 months ago 13
Swift Question

How to convert string to UInt32?

I am a beginner in swift and I am having a problem with convering string to UInt32.

let Generator = (ReadableJSON ["People"] [Person]["F1"].string! as NSString).doubleValue

if Generator == 1 {

NameLabel1 = ReadableJSON ["People"] [Person]["A1"].string as String!
NameImeNaObekt = ReadableJSON ["People"] [Person] ["B1"].string as String!
Picture = ReadableJSON ["People"] [Person] ["E1"].string as String!

} else {

let RGen = arc4random_uniform ("\(Generator)") // here is the error

}


Would you advise me how to fix it. The problem is in the last line, which is red and it says Cannot convert value of type String to UInt32.
The main idea is that I am reading the number from a JSON file and I have to populate this number into the arc4random_uniform.

Answer
arc4random_uniform(UInt32) 

accept an UInt32 value but you are passing an String value to it

this converts your number to string

"\(Generator)"

the last line should be like this

let RGen = arc4random_uniform (UInt32(Generator))

and if you want to 'RGen' is an String you can do it this way

"\(RGen)"
String(RGen)