1

Trying to make a function in Swift Spritekit, this is how it looks like:

func card_vals(card_value_set: [Int], card_set: [String]) {
        var card_value_set = card_value_set
        var card_set = card_set
        for i in card_set {
            var j = Int(i[0])
                card_value_set.append(j)
            }
    }
 

In simple words, the function is taking a card_set, looking like ["3C", "4D"] which is a Str list, and the card_value_set should look like [3,4], which is a Int list. However, I am getting the following error:

cannot convert value of type 'Int' to expected argument type 'Range<String.Index>

Any help to erase this error?

Thanks

1 Answers1

0

You need to the wrapped with String

i[0] return character and there is no initializer of Int type for accepting character type.

func card_vals(card_value_set: [Int], card_set: [String]) {
    var card_value_set = card_value_set
    var card_set = card_set
    for i in card_set {
        if let first = i.first, let intData = Int(String(first)) {
            card_value_set.append(intData)
        }
    }
}

Or you can also do this

var j = Int(String(i[0])) ?? 0
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52