ho questa espressione che restituisce un UInt32
:Swift convertito UInt a int
let randomLetterNumber = arc4random()%26
voglio essere in grado di utilizzare il numero in questo if:
if letters.count > randomLetterNumber{
var randomLetter = letters[randomLetterNumber]
}
Questo problema è che il console mi sta dando questa
Playground execution failed: error: <REPL>:11:18: error: could not find an overload for '>' that accepts the supplied arguments
if letters.count > randomLetterNumber{
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Il problema è che non può UInt32
essere confrontato con un Int
. Voglio trasmettere randomLetterNumber
a Int
. Ho provato:
let randomLetterUNumber : Int = arc4random()%26
let randomLetterUNumber = arc4random()%26 as Int
Questi sia causa could not find an overload for '%' that accepts the supplied arguments.
Come posso cast del valore e non utilizzarlo in if?
Grazie per questo, l'inizializzatore Int() sembra fare il trucco. – 68cherries
Puoi leggere ulteriori informazioni sulla conversione del tipo numerico in ** [Documenti di Apple Swift] (https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/TheBasics.html#//apple_ref/ doc/uid/TP40014097-CH5-XID_420) **. –
Grazie. Ho avuto un problema simile con arc4random_uniform (someArray.count) soluzioni di fusione il problema arc4random_uniform (UInt32 (someArray.count)) – nacross