在swift playground中,此代码可以正常工作:
ul#menu-mainmenu {
display:table;
width: 100%;
margin:0 0;}
ul#menu-mainmenu li {
width: auto;
margin: 0 30px 0 20px;}
但是,如果我们 REPLACE 下面四行中的每一行,上面的第二行,则不会生成编译错误,但shoppingList = ["item0", "item1", "item2", "item3"]
shoppingList[3...3] = ["item4", "item5", "item6"]
shoppingList.count // prints 6.
shoppingList // prints item0 through item6 (minus item3) in the shoppingList.
不打印任何内容:
shoppingList.count
对我而言,如果由于某种原因shoppingList[3...4] = ["item4", "item5", "item6"] // Doesn't Work!
shoppingList[3...6] = ["item4", "item5", "item6"] // Doesn't Work!
shoppingList[4...6] = ["item4", "item5", "item6"] // Doesn't Work!
shoppingList[3...5] = ["item4", "item5", "item6"] // Doesn't Work!
正在附加到数组中,那么上面三行中的至少一行应该可以正常工作。
答案 0 :(得分:3)
全部
shoppingList[3...4] = ["item4", "item5", "item6"] // Doesn't Work!
shoppingList[3...6] = ["item4", "item5", "item6"] // Doesn't Work!
shoppingList[4...6] = ["item4", "item5", "item6"] // Doesn't Work!
shoppingList[3...5] = ["item4", "item5", "item6"] // Doesn't Work!
导致fatal error: Array index out of range
因为4
不是具有4个元素的数组的有效索引。
如果你想使用下标setter附加到数组
(“拼接”)然后你可以使用
排除上限的..<
范围运算符:
var shoppingList = ["item0", "item1", "item2", "item3"]
shoppingList[4 ..< 4] = ["item4", "item5", "item6"]
// ["item0", "item1", "item2", "item3", "item4", "item5", "item6"]
当然,也可以使用+=
或appendContentsOf()
。