What is the difference between unshare () and copy ()?

Both types unshare() and copy() are used to copy the array, but I do not see the difference.

+7
arrays swift
source share
5 answers

open list

As stated in the Apple Documentation, unshare consistent with ensuring that the copy of the array is unique , so when you call unshare you get a non-shared copy of the array, it could be the same array if it is the only reference to the array.

Copy

On the other hand, copy forcibly copies the array and returns a new array containing the copied elements .

So, if you want to have 2 independent copies of the array, you must use copy in another case, you can use unsare to ensure that the array has no other references.

+4
source share

unshare() do nothing if the called user is no longer in use. copy() will copy it independently.

+4
source share

This has changed since Beta 3. unshare () and copy () have been removed from the array. Now arrays are now copied by write, and the default behavior is very similar to calling unshare ().

http://blog.human-friendly.com/swift-arrays-beta-3-hooray

+3
source share

From the documentation :

If you just need to be sure that your reference to the contents of the arrays is the only link, call the unshare method, not the copy method. The unshare method does not create a copy of the array unless necessary. The copy method always copies the array, even if it is no longer split.

Basically, unshare will do nothing if it is the only link. A copy will do this anyway.

+2
source share

In the quick programming guide (p. 298):

β€œIf you just need to be sure that your reference to the content arrays is the only link available, call the unshare method, not the copy method. The unshare method does not copy the array if it is not necessary. The copy method always copies the array, even if it’s not already divided. "

Excerpt from: Apple Inc. "Fast programming language." interactive books. https://itun.es/us/jEUH0.l

+2
source share

All Articles