How to create Swift empty two dimensional array with size

前端 未结 5 1542

I try to do smth like this:

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

where [5,5] is size of array. I can\'t do this.

相关标签:
5条回答
  • 2020-12-28 19:34

    If you want to make a multidimensional array of value types (i.e. Ints, Strings, structs), the syntax in codester's answer works great:

    Swift 4

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

    Swift Earlier

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

    If you make a multidimensional array of reference types (i.e. classes), this gets 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 make an array (uni- or multidimensional) of reference types, you might be better off either making 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's answer for equivalent shorter syntax using map().)

    Or making an array of optionals and filling 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], ...
    
    0 讨论(0)
  • 2020-12-28 19:35

    For two dimensional array with different dimension of each component (not square matrix) you may write

    var arr2D: [[Int]] = [Array<Int>(repeating: 1, count:2), Array<Int>(repeating: 2, count:3)]
    
    0 讨论(0)
  • 2020-12-28 19:37

    You can do this by following code it will intialize the two dimensional array with same default value which in this case is same instance of MyClass object and this is not that great.In case you need different objects you need to make for-loop to intialize the objects.But in case for same instance the following code will work.This code is better for primitive type like Int or structs String but not for Classes.But in case i am showing the syntax for two dimensional array intialization

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

    better would be to use primitive data types and stuct

    It will fill the array with two dimentional MyClass objects.As default value is needed in Swift if you are providing size. From Swift programming guide

    “Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to a provided default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type ”


    As of now i do not know any way to just intialize its memory but not to fill the array in Swift of certain size.Above code will fill default MyClass objects of 5*5.

    0 讨论(0)
  • 2020-12-28 19:41

    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 arguments and repeating instead of repeatedValue.

    0 讨论(0)
  • 2020-12-28 19:50

    Creating 5x5 array filled with distinct objects of the same class. Maybe not the prettiest solution, but a working one, and avoiding the for loops:

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

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

    0 讨论(0)
提交回复
热议问题