In Swift, when you pass a value type, say an array for the function. A copy of the array is for using the function.
However, the documentation at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_134 also says:
The description above refers to "copying" strings, arrays, and dictionaries. The behavior that you see in your code will always be as if the copy took place. However, Swift only performs an actual copy of the scene when absolutely necessary. Swift manages everything to ensure optimal performance, and you should not avoid trying to eliminate this optimization.
Does this mean that copying is actually performed only when the type of the passed value is changed?
Is there a way to demonstrate that this is actually the main behavior?
Why is it important? If I create a large immutable array and want to pass it from function to function, I certainly do not want to make copies. Should I just use NSArrray in this case or will the Swift Array work fine until I try to manipulate the passed in array?
Now, until I explicitly change the variables in a function edited with var or inout, then the function cannot change the array in any case. So is he still making a copy? It is provided that another thread can change the original array in another place (only if it is changed), making a copy at the moment the function is called necessary (but only if the array passed to is changed). Therefore, if the original array is immutable and the function does not use var or inout, there is no point in creating a copy in Swift. Correctly? So what does Apple mean by the phrase above?
swift
Barka
source share