Doubt, add value to an array [AnyObject] using Swift?

1

How would I do this in swift?

var jogador02 = ["Teste", 0]

jogador02[1] += 1

Result equals

jogador02 = ["teste", 1]
    
asked by anonymous 21.01.2016 / 18:11

1 answer

2

As you have mixed the types of your array, everything becomes AnyObject , so to do the sum binary operation you need to type the given.

var jogador02 = ["Teste", 0]
var temp = jogador02[1] as! Int
temp += 1 // Pode substituir por temp++
jogador02[1] = temp

I would recommend that you create a player class with the desired attributes, in case it looks like this:

class Jogador {

  var nome = ""
  var pontuacao = 0    

}

And when to use:

var jogador = Jogador()
jogador.pontuacao += 1
    
21.01.2016 / 18:32