简体   繁体   English

Swift 3-如何初始化多维字符串数组?

[英]Swift 3 - How do you initialise a multi-dimensional string array?

If I want to create a four dimensional string array I can type: 如果要创建一个四维字符串数组,可以输入:

var array4 = [[[[String]]]]() 

If I want to initialise a single dimensional array I can use: 如果要初始化一维数组,可以使用:

var array1 = [String](repeating: nil, count 10)

So how do I initialise a four dimensional string array? 那么如何初始化一个四维字符串数组呢?

var array4 = [[[[String]]]](repeating: nil, count: 10)

Doesn't work and gives an "Expression type is ambiguous" error. 无效,并显示“表达式类型不明确”错误。

You are wrong your declaration of array1 will also give you error. 您错了,您对array1的声明也会给您错误。

var array1 = [String](repeating: nil, count: 10) //This will not compile

Will also give you error 也会给你错误

Expression type '[String]' is ambiguous without more context" 表达式类型'[String]'含糊不清,没有更多上下文”

Because you cannot set nil value to String you can set it String? 因为您不能将nil值设置为String ,所以可以将其设置为String? , So you need to declare array with type [String?] . ,因此您需要声明[String?]类型的数组。

var array1 = [String?](repeating: nil, count: 10) //This will compile

Same goes for array4 . array4 You can declare array4 like this way 您可以这样声明array4

var array4 = [[[[String]]]?](repeating: nil, count: 10) //With nil objects

Or 要么

var array4 = [[[[String]]]](repeating: [], count: 10) //With empty array objects

I expect that you want to actually do is declare a 4D array of a specified size, and then fill it by referring to the indices a[0][1][0][1] = "something" . 我希望您实际要做的是声明一个指定大小的4D数组,然后通过引用索引a[0][1][0][1] = "something"填充它。

Let's suppose for the sake of example that you want a string at each corner of a hypercube (the only "concrete" example I can think of off the top of my head for a 4D array is 4D geometry). 让我们以示例为例,您想在超立方体的每个角上都需要一个字符串(对于4D数组,我能想到的唯一“具体”示例就是4D几何)。 Then the vertex indices are going to be [0,0,0,0] [0,0,0,1] ... [1,1,1,0] [1,1,1,1] - 16 in total. 然后顶点索引将是[0,0,0,0] [0,0,0,1] ... [1,1,1,0] [1,1,1,1]-16 in总。 Long-hand (possible for length = 2) we have: 长手(可能的长度= 2),我们有:

var a: [[[[String]]]]
a = [
        [
            [
                ["?", "?"], ["?", "?"]
            ],
            [
                ["?", "?"], ["?", "?"]
            ],
        ],
        [
            [
                ["?", "?"], ["?", "?"]
            ],
            [
                ["?", "?"], ["?", "?"]
            ],
        ]
    ]

Now, we notice that each dimension is just a copy of the previous dimension n times (2 in this case), so to do it generically let's define: 现在,我们注意到每个维度仅是前一个维度的副本n次(在这种情况下为2),因此一般而言,我们来定义一下:

func arrayOf<T>(el: T, count: Int) -> [T] {
    return [T](repeatElement(el, count: count))
}

It would be lovely to use recursion with a function like: 将递归与以下函数一起使用将是很不错的:

// func arrayOfArrays<T>(el: T, count: Int, depth: Int) -> ??? {
//     if depth == 1 { return arrayOf(el: el, count: count) }
//     return arrayOf(el: arrayOfArrays(el: el, count: count, depth: depth - 1), count: count)
// }
// a1:[[[[String]]]] = arrayOfArrays(el:"?", count: 2, depth: 4)

but what does it return? 但是它返回什么? Generics can't cope with this, so we have to do it longhand: 泛型无法解决此问题,因此我们必须长期进行:

var a1: [[[[String]]]]

a1 = arrayOf(el: arrayOf(el: arrayOf(el: arrayOf(el: "?", count: 2), count: 2), count: 2), count: 2)

a1[0][0][0][0] // "?" as expected
a1[1][1][1][1] = "!?"
a1[1][1][1][1] // "!?" as required
typealias TwoDimensionArray<T> = [Array<T>]
typealias ThreeDimensionArray<T> = [TwoDimensionArray<T>]
typealias FourDimensionArray<T> = [ThreeDimensionArray<T>]

var array4 = FourDimensionArray<String>.init(repeating: [], count: 10)

print(array4) // [[], [], [], [], [], [], [], [], [], []]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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