0

I have defined a two dimensional array as :

var monthNames:[[String]]

I want to initialize it as following:

Number of columns: 3, and rows of each column: undefined

so I can init rows later.

2 Answers 2

3

As per your question, You are creating array with three columns, each columns will be having more number of rows(records), SO you can do as follows:

var twoDimesions: [[String]] = [[], [], []]

var array1: [String]=[]
array1.append("1 name1")
array1.append("1 name2")
twoDimesions[0] = array1

var array2: [String]=[]
array2.append("2 name1")
array2.append("2 name2")
twoDimesions[1] = array2

println("twoDimentions: \(twoDimesions)")
println("array1: \(array1)")
println("array2: \(array2)")

and you will be getting log as follows:

twoDimentions: [[1 name1, 1 name2], [2 name1, 2 name2], []]
array1: [1 name1, 1 name2]
array2: [2 name1, 2 name2]

This may help you!!!

Sign up to request clarification or add additional context in comments.

Comments

1

I presume by undefined you mean empty - so it's an array of 3 elements, where each element is an (empty) array. You can initialize as follows:

var monthNames:[[String]] = [ [], [], [] ]

or:

var monthNames = [[String]](count: 3, repeatedValue: [])

Side note: if you want to initialize the arrays later, remember that you cannot do this:

var array = monthNames[0]
array.append("another test")

because arrays are value types, so when assigned to a variable, a copy of the original array is done. Any change you made is local to that variable, hence not reflected in your original array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.