编译时的数组元素数

时间:2014-12-10 11:14:41

标签: arrays swift compile-time

我想知道编译时数组元素的数量:

let sections = ["A","B","C"]

// I want fill later this Array with Arrays of type Product
var productsInSection = [[Product]](count:number of Array elements from sections array, repeatedValue:[])

我的问题是我可以用于:“数组中的数组元素” 如果我随时改变,我不必看看

let sections = ["A","B","C","D"]

1 个答案:

答案 0 :(得分:1)

我不明白为什么你需要“编译时间”。

但我认为count property足以满足您的需求:

let sections = ["A","B","C"]
var productsInSection = [[Product]](count:sections.count, repeatedValue:[])

已添加回答评论

例如:

private let _indexSectionTitles = ["A","B","C","D","E","F","G","H","I","J","K","L","M", "N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "Ä","Ö","Ü", "#" ]

class MyClass{
    let indexSectionTitles = _indexSectionTitles
    var productsInSection = [[GlobalProduct]](count: _indexSectionTitles.count, repeatedValue: [])
}