0

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.

Dakata
  • 1,227
  • 2
  • 14
  • 33
  • 1
    Since you are a beginner in Swift please learn first to consider the naming convention that variable names start with a lowercase letter. – vadian Sep 23 '16 at 19:43

2 Answers2

1
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)
Arashk
  • 617
  • 5
  • 16
-1
     var RGen= 0

     let RGen =int( arc4random_uniform ("\(Generator)")  )

or let RGen =( arc4random_uniform ("(Generator)") ).toInt

Look here

Community
  • 1
  • 1
GDutton
  • 164
  • 14