我有此表达式返回一个UInt32:
UInt32
let randomLetterNumber = arc4random()%26
我希望能够在此if语句中使用数字:
if letters.count > randomLetterNumber{ var randomLetter = letters[randomLetterNumber] }
问题是控制台给了我这个
Playground execution failed: error: <REPL>:11:18: error: could not find an overload for '>' that accepts the supplied arguments if letters.count > randomLetterNumber{ ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
问题是UInt32无法与进行比较Int。我想投到randomLetterNumber一个Int。我努力了:
Int
randomLetterNumber
let randomLetterUNumber : Int = arc4random()%26 let randomLetterUNumber = arc4random()%26 as Int
这些都导致 could not find an overload for '%' that accepts the supplied arguments.
could not find an overload for '%' that accepts the supplied arguments.
如何转换值或在if语句中使用它?
Int(arc4random_uniform(26)) 做两件事,一是消除当前方法的负面结果,二是应根据结果正确创建一个Int。
Int(arc4random_uniform(26))