Swift convert UInt to Int
Solution 1
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.
Solution 2
More simple than this, impossible:
Int(myUInteger)
Solution 3
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)
Solution 4
You can do
let u: UInt32 = 0x1234abcd
let s: Int32 = Int32(bitPattern: u)
67cherries
I've been programming for iOS for about four years. I've worked with objective-c, swift, python, java and javascript SO milestones: 6576th to the Strunk & White Badge :D
Updated on July 19, 2022Comments
-
67cherries almost 2 years
I have this expression which returns a
UInt32
:let randomLetterNumber = arc4random()%26
I want to be able to use the number in this if statement:
if letters.count > randomLetterNumber{ var randomLetter = letters[randomLetterNumber] }
This issue is that the console is giving me this
Playground execution failed: error: <REPL>:11:18: error: could not find an overload for '>' that accepts the supplied arguments if letters.count > randomLetterNumber{ ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
The problem is that
UInt32
cannot be compared to anInt
. I want to castrandomLetterNumber
to anInt
. I have tried:let randomLetterUNumber : Int = arc4random()%26 let randomLetterUNumber = arc4random()%26 as Int
These both cause
could not find an overload for '%' that accepts the supplied arguments.
How can I cast the value or use it in the if statement?
-
David Berry almost 10 yearsNote that this will throw an exception half the time due to overflow checking, use the answer I gave to avoid that.
-
67cherries almost 10 yearsThanks for that, the Int() initializer seems to do the trick.
-
Firo almost 10 years@David, how so? Not disagreeing, just want to understand what you mean.
-
Nate Cook almost 10 yearsYou can read more about numeric type conversion in Apple's Swift docs.
-
David Berry almost 10 yearsActually it won't since you're doing the casting after the modulo, so you can guarantee it's always in bounds.
Int(arc4random())
will crash 50% of the time it's executed on a 32-bit platform because a UInt32 won't fit in an Int32. Just seen too many "arc4random is bugged and crashes" questions with swift. In any case arc4random_uniform will give a more uniform distribution. -
nacross almost 10 yearsThanks. I had a similar problem with arc4random_uniform(someArray.count) casting fixes the problem arc4random_uniform(UInt32(someArray.count))
-
cbowns over 8 yearsModifying/bounding the output of a random number distribution with modulo or division operations is poor form: it ruins the distribution properties that the functions guarantee. Instead, use
arc4random_uniform(26)
to let the function uphold a uniform distribution across the range you specify. -
bshirley over 8 yearsI had both problems: probability = Int(arc4random_uniform(UInt32(total))) … the main problem is because of the headers the type-ahead doesn't have info on this method and doesn't provide the needed metadata (which it would then offer as suggestions to fix the problem)
-
0xKayvan about 6 yearsThis does the exact opposite of what OP asked. Should be
Int(myUInteger)
-
João Serra over 3 yearsthis suddenly start giving me "Not enough bits to represent the passed value" more specifically Int(arc4random())