2014-06-05 80 views
0

我不知道这是否操场bug或者它应该像这样的工作:游乐场错误输出

var types = ["0", "1", "2"]  // ["0","1","2"] 
    types += "3"     // ["0","1","2","3"] 
    types += ["4", "5"]   // ["0","1","2","3","4","5"] 
    types[3..5] = ["34"]   // ["34"] 

在我看来在最后一行types应该包含["0","1","2","34","5"],但操场给出不同的输出 - 写在右边。

我认为在右边我们只能看到最后编辑过的东西,但是在第二行中我们可以看到整个类型的数组。

在助理编辑器中,我得到了[0] "34",而应该是[3] "34"和我认为其余的数组。

+0

我想它显示了LHS的结果。在你的最后一行中,将是'types [3..5]'这是(在赋值后)'[“34”]'。这也与你用助理编辑器获得的'[0]“34”一致。 – fizruk

回答

2

你所看到的原因只有["34"]types[3..<5] = ["34"]行是因为赋值运算符=返回已分配的值。

其他行显示整个数组,因为+=运算符返回赋值结果。

2

var指的是可变内容并且您还将为其分配值。

types[] - 索引处的新值,意味着它不应该连接。

对于防爆:

var types = ["0", "1", "2"] 
types += "5" 
types += ["4", "5"] 
types[3..5] = ["34"] // Here considering the index of 3..5 (3 & 4) as one index - Assigning a single value and replaced with the value 
types 

enter image description here