Playground wrong output
-
26-12-2019 - |
Question
I wonder if this is playground bug or it's supposed to work like this:
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"]
In my opinion in last line types
should contain ["0","1","2","34","5"]
, however playground gives different output - written on the right.
I would consider that on the right we can see only last edited stuff, but in line 2&3 we can see whole types array.
In assistant editor I get [0] "34"
, while it should be [3] "34"
and rest of array in my opinion.
Solution
The reason you are seeing only ["34"]
after the types[3..<5] = ["34"]
line is because the assignment operator =
returns the value that has been assigned.
The other lines show the whole array because the +=
operator returns the result of the assignment.
OTHER TIPS
var
refers to the mutable content and also you'r re assigning the values to it .
types[]
- new value at index , mean it shouldn't be concatenate .
For Ex :
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