Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between unshare() and copy()?

Tags:

arrays

swift

Both unshare() and copy() are used for copying an array, but I can't see the difference.

like image 710
lazyd3v Avatar asked Jun 04 '14 18:06

lazyd3v


2 Answers

Unshare

As it says on Apple documentation unshare is to ensure that the copy of the array is unique, so when you call unshare you get non shared copy of the array, that could be the same array if that was the only reference to the array.

Copy

On the other hand copy forces the copy of the array and returns a new array containing the copied items.

So if you want to have 2 independent copies of the array you should use copy on other case you can use unsare to ensure the array has no other references.

like image 193
pconcepcion Avatar answered Oct 09 '22 10:10

pconcepcion


unshare() will do nothing if the callee is already not shared. copy() will copy it regardless.

like image 36
Fernando Mazzon Avatar answered Oct 09 '22 10:10

Fernando Mazzon