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.

1
  • 1
    Since you are a beginner in Swift please learn first to consider the naming convention that variable names start with a lowercase letter. Commented Sep 23, 2016 at 19:43

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

The number behind the generator should be displayed into arc4random_uniform
as your 'Generator' is double so your code should be like this arc4random_uniform(UInt32(Generator))
-1
     var RGen= 0

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

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

Look here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.