Conflicting definition of Swift struct and array -
in swift programming language, says:
“all of basic types in swift—integers, floating-point numbers, booleans, strings, arrays , dictionaries—are value types, , implemented structures behind scenes.”
“structure instances always passed value, , class instances passed reference”
“if assign array instance constant or variable, or pass array instance argument function or method call, contents of array not copied @ point assignment or call takes place. instead, both arrays share same sequence of element values. when modify element value through 1 array, result observable through other.”
clearly, swift contradicting when comes array. array value type or reference type?
array special cases. value types special behavior.
at page found point 3) there enough information understand why:
the assignment , copy behavior swift’s array type more complex dictionary type. array provides c-like performance when work array’s contents , copies array’s contents when copying necessary
and, after statement:
instead, both arrays share same sequence of element values. when modify element value through 1 array, result observable through other.
for arrays, copying takes place when perform action has potential modify length of array.
so, reading these points understand that:
- array struct special behavior, reason conserve c-like performance.
- the copy made when length of copied array changed
last thing: remember that, when array copied, value types (structures, enumerations, primitive types) copied. objects not copied, references (pointers) copied
Comments
Post a Comment