Adam Kalnas
Adam Kalnas

Reputation: 1198

How can I declare a two dimension array with different types in swift?

Two dimension array - Ints

// Works!
var foo = [[Int]]() 

Two dimension array - Ints / Strings

// Neither work.  Halp!

var foo = [String][Int]() 
var foo = [String[Int]]() 

I found this question, which leads me to believe that this can be done but is not advised.

Upvotes: 0

Views: 3776

Answers (1)

tmac_balla
tmac_balla

Reputation: 648

The best practice here would be to use a tuple of two values of different types

var foo:[(String, Int)] = []

Or if you want to use values of different types

var foo:[(AnyObject, AnyObject)] = []

But if you want it to be a multidimensional array anyway, then just make it not type-specific:

var foo:[[AnyObject]] = []

Upvotes: 7

Related Questions