之前已经知道了如何初始化一位数组的值了:
但是现在遇到了:
想要初始化二维数组的值,写成:
var contentList:[[String]] init(){ self.contentList = Array<<String>>(count: 20, repeatedValue: [1, 2, 3, 4, 5, 6, 7]) |
结果报错:
Non-associative operator is adjacent to operator of same precedence
写成:
self.contentList = Array<[String]>(count: 20, repeatedValue: [1, 2, 3, 4, 5, 6, 7]) |
报错:
Cannot invoke initializer for type ‘Array<[String]>’ with an argument list of type ‘(count: Int, repeatedValue: [Int])’
swift two dimension array init with capacity
swift two dimensional array init with capacity
let points: [[Int]] = [[10, 20], [30, 40]] |
->此处我不能手写初始化值,否则就累死了。。。
Two-dimensional array in Swift – Stack Overflow
self.contentList = Array(count: 20, repeatedValue: [“1”, “2”, “3”, “4”, “5”, “6”, “7”]) |
也是可以的。
或者是:
self.contentList = Array(count: 20, repeatedValue: Array(count: 7, repeatedValue: “1”)) |
[总结]
二维数组的初始化,就是:
Array(count:, repeatedValue:) 去嵌套的初始化即可:
self.contentList = Array(count: 20, repeatedValue: Array(count: 7, repeatedValue: “1”)) |
或者是:
self.contentList = Array(count: 20, repeatedValue: [“1”, “2”, “3”, “4”, “5”, “6”, “7”]) |
转载请注明:在路上 » [已解决]swift中初始化二维数组的值