我正在猜测纸牌游戏,我得到了这个奇怪的错误,当我为符号和卡号创建一个随机数时,我无法修复。这是错误:
let cardSymbols = ["Spades", "Hearts", "Diamonds", "Clubs"]
let numbers = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
func getRandomCard() {
var correctSymbolNumber = arc4random_uniform(UInt32(cardSymbols.count - 1))
var correctNumberNumber = arc4random_uniform(UInt32(numbers.count - 1))
var correctSymbol = cardSymbols[correctSymbolNumber]
var correctNumber = numbers[correctNumberNumber]
}
我该如何解决这个问题?我知道问题出在我的arc4random上......但我该如何解决呢。
答案 0 :(得分:1)
你需要将arc4random_uniform的结果更改为swift Int,目前它是一个你不能用于swift字典或数组的UInt32。
所以:
var correctSymbolNumber = Int(arc4random_uniform(UInt32(cardSymbols.count - 1)))
var correctNumberNumber = Int(arc4random_uniform(UInt32(numbers.count - 1)))
var correctSymbol = cardSymbols[correctSymbolNumber]
var correctNumber = numbers[correctNumberNumber]
答案 1 :(得分:0)
您必须将arc4random_uniform UInt32的值更改为Int。正如您所看到的,这是返回UInt32的arc4random_uniform
函数的原型
func arc4random_uniform(_: UInt32) -> UInt32
var correctSymbolNumber = Int(arc4random_uniform(UInt32(cardSymbols.count - 1)))
var correctNumberNumber = Int(arc4random_uniform(UInt32(numbers.count - 1)))
也不要忘记打开correctSymbolNumber
和correctNumberNumber
值。