ios - Are optional arrays designed to be "immutable"? -
as found in documentation, arrays declared var mutable default
var a: int[] = [1,2,3] += 4 //[1,2,3,4]
but never found reason, why optional arrays immutable (should , why). these 2 examples illustates concern:
var a: int[]? = [1,2,3] a! += 4 // error: not find operator += etc. - .append not work
this gives error, 1 not
var a: int[]? = [1,2,3] var b = a! b += 4 = b // [1,2,3,4]
and performs expected. there particular reason, why cannot done in 1 line, in previous example? far see that, underlaying array constructed let, not makes sense me yet.
your var of type int[]? whilst var b not - implicitly of type int[], since set equal unwrapped value of a.
b can therefore appended (it's var array), whilst cannot (it's var, not array - it's optional wrapping array), but, int[]? can assign modified b it. append optional array have "unwrap" lhs like
a! += 4 // doesn't work
Comments
Post a Comment