简体   繁体   中英

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.

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]] = []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM