How to create a Swift empty two-dimensional array with size

I am trying to do something like this:

let myArray: [[MyClass]] = [5,5] 

where [5,5] is the size of the array. I can not do it.

+9
arrays swift
Jul 17 '14 at
source share
5 answers

If you want to create a multidimensional array of value types (i.e. Int s, String s, structs), the syntax in the response to the code word works fine:

Swift 4

 var arr = [[Int]](repeating: [Int](repeating: 0, count: 5), count: 5) 

Fast early

 var arr = [[Int]](count: 5, repeatedValue: [Int](count: 5, repeatedValue: 0)) arr[0][1] = 1 // arr is [[0, 1, 0, 0, 0], ... 

If you create a multidimensional array of reference types (e.g. classes), this gives you an array of many references to the same object:

 class C { var v: Int = 0 } var cArr = [[C]](count: 5, repeatedValue: [C](count: 5, repeatedValue: C())) // cArr is [[{v 0}, {v 0}, {v 0}, {v 0}, {v 0}], ... cArr[0][1].v = 1 // cArr is [[{v 1}, {v 1}, {v 1}, {v 1}, {v 1}], ... 

If you want to create an array (one- or multi-dimensional) of reference types, you might be better off or make the array dynamically:

 var cArr = [[C]]() for _ in 0..<5 { var tmp = [C]() for _ in 0..<5 { tmp += C() } cArr += tmp } // cArr is [[{v 0}, {v 0}, {v 0}, {v 0}, {v 0}], ... cArr[0][1].v = 1 // cArr is [[{v 0}, {v 1}, {v 0}, {v 0}, {v 0}], ... 

(see slazyk answer for the equivalent shorter syntax using map() .)

Or create an array of options and fill in their values:

 var optArr = [[C?]](count: 5, repeatedValue: [C?](count: 5, repeatedValue: nil)) // optArr is [[nil, nil, nil, nil, nil], ... optArr[0][1] = C() // optArr is [[nil, {v 0}, nil, nil, nil], ... 
+20
Jul 17 '14 at 19:39
source share
— -

Creating a 5x5 array filled with individual objects of the same class. This may not be the nicest solution, but working and avoiding for loops:

 let myArray = [[MyClass]](map(0..<5) { _ in [MyClass](map(0..<5) { _ in MyClass() }) }) 

Edit:
Since the question was to create an "empty" array with a "size", I must add that you cannot "empty" it if it is not an optional type. But if that's what you want, you can also do it as rikster suggests at the bottom of your answer and just create a 5x5 nil array.

+4
Jul 17 '14 at 19:39
source share

To add to the accepted answer, in Swift 3 the syntax is slightly different:

 var arr = [[Int]](repeating: [Int](repeating: 0, count: 5), count: 5) 

Note the order of the arguments and repeating instead of repeatedValue .

+4
Jan 09 '17 at 11:48 on
source share

You can do this by running the following code: it will initialize a two-dimensional array with the same default value, which in this case is the same instance of the MyClass object , and this is not so important. If you need various objects that need to be done to make a for-loop to initialize the objects. But in the case for the same instance, the following code will work. This code is better suited for a primitive type such as Int or structs String , but not Classes . But in case I show the syntax for two-dimensional massive initialization

 let myArray: [[MyClass]] = [[MyClass]](count: 5, repeatedValue:[MyClass](count: 5, repeatedValue:MyClass())); 

it would be better to use primitive data types and stuct

It will populate the array with two dimensional MyClass objects. Because the default value is needed in Swift if you provide a size. From the Swift Programming Guide

"The Swifts Array type also provides an initializer for creating an array of a certain size with all its values ​​set to the default value. You pass this initializer the number of elements to add to the new array (called the counter) and the default value of the corresponding type"




At the moment, I do not know of any way to simply intialize its memory, but not to fill an array in Swift certain size. Each code will populate the default objects MyClass 5 * 5.

+2
Jul 17 '14 at 19:14
source share

For a two-dimensional array with different dimensions of each component (not a square matrix), you can write

 var arr2D: [[Int]] = [Array<Int>(repeating: 1, count:2), Array<Int>(repeating: 2, count:3)] 
0
Jun 02 '17 at 5:40 on
source share



All Articles