[英]Swift append to 2d array
我试图在Swift中将值附加到2d数组中,但在第8行上却抛出“索引超出范围”错误
private var cards : [[Int]] = [[]]
init() {
//Fill cards array by adding all cards
for i in 0...12{
for x in 0...3{
cards[0].append(i+2) //append card number... 2,3,4,5 etc
cards[1].append(x) //append card type... hearts, diamonds, clubs and spades
//with a value which represents it (0, 1, 2 and 3)
}
}
}
您不能使用cards[0]
访问cards
的内部数组,因为您将cards
初始化为一个空数组,因此cards.count = 0
,所以cards[0]
不存在。
private var cards = [[Int]]()
init() {
//Fill cards array by adding all cards
for i in 0...12{
for x in 0...3{
cards.append([i+2,x])
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.