Swift convert UInt to Int

前端 未结 4 1593
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 14:19

I have this expression which returns a UInt32:

let randomLetterNumber = arc4random()%26

I want to be able to use the number in

相关标签:
4条回答
  • 2020-12-14 14:29

    Just create a new int with it

    let newRandom: Int = Int(randomLetterNumber)
    if letters.count > newRandom {
        var randomLetter = letters[newRandom]
    }
    

    or if you never care about the UInt32 you can just create an Int immediately:

    let randomLetterNumber = Int(arc4random() % 26)
    
    0 讨论(0)
  • 2020-12-14 14:43

    More simple than this, impossible:

    Int(myUInteger)
    
    0 讨论(0)
  • 2020-12-14 14:50

    Int(arc4random_uniform(26)) does two things, one it eliminates the negative results from your current method and second should correctly creat an Int from the result.

    0 讨论(0)
  • 2020-12-14 14:52

    You can do

    let u: UInt32 = 0x1234abcd
    let s: Int32 = Int32(bitPattern: u)
    
    0 讨论(0)
提交回复
热议问题