-2

I am making a guess the card game and i get this weird error which I cannot fix when I create a random number for the symbols and the card numbers. This is the error:

An image of the error

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]

}

How can I fix this? I know the problem is with my arc4random... but how can I fix it.

Sahil
  • 9,096
  • 3
  • 25
  • 29

2 Answers2

1

You need to change the result of arc4random_uniform to a swift Int, currently it is a UInt32 which you cannot use for a swift dictionary or array.

So:

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]
Olivier Wilkinson
  • 2,836
  • 15
  • 17
0

You have to change the value of arc4random_uniform UInt32 to Int. as you can see this is the prototype of arc4random_uniform function which returns UInt32 func arc4random_uniform(_: UInt32) -> UInt32

var correctSymbolNumber = Int(arc4random_uniform(UInt32(cardSymbols.count - 1)))
var correctNumberNumber = Int(arc4random_uniform(UInt32(numbers.count - 1)))

also don't forget the unwrap thecorrectSymbolNumber and correctNumberNumber values.

Sahil
  • 9,096
  • 3
  • 25
  • 29