# How do I copy an array within and object?

**URL:** <https://forums.kodeco.com/t/how-do-i-copy-an-array-within-and-object/38951>\
**Category:** Swift\
**Created:** [January 28, 2018, 6:09am UTC](https://forums.kodeco.com/t/how-do-i-copy-an-array-within-and-object/38951 "2018-01-28T06:09:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jport1130](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jport1130/32/102075_2.png) [@jport1130](https://forums.kodeco.com/u/jport1130)\
**Post date:** [January 28, 2018, 6:09am UTC](https://forums.kodeco.com/t/how-do-i-copy-an-array-within-and-object/38951/1 "2018-01-28T06:09:32Z")

</div>

I have an array of objects and each object contains another array. How do I copy the array within the object? Using NSCopying I have been able to copy the object but the array within it will not copy.

![Snip20180127_1](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/5/1/515fdc2af4fe602898df11c2adc01aa860b89fb5.png)

![Snip20180127_2](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/f/0/f054d032bde4c93f13545bdde1ecb52a9a355c94.png)

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [January 28, 2018, 8:04am UTC](https://forums.kodeco.com/t/how-do-i-copy-an-array-within-and-object/38951/2 "2018-01-28T08:04:24Z")

</div>

Hi John, @jport1130  
To give you a truer understanding of the problem, it is a distinction between Shallow Copy and Deep Copy. That is the issue here. Since everything (in computer terms) is a memory pointer that holds an address to the memory where the data is stored, not everything is copied/duplicated.

Apple docs suggest using NSCoding for a true deep copy of objects.

I will try to find some more detailed information for you.

You can read a bit more on [Copying Collections](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Collections/Articles/Copying.html#//apple_ref/doc/uid/TP40010162-SW1)

P.S. I am trying to provide you a way to post your project to allow someone(me as in your other question) to have a look at it.

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [January 29, 2018, 2:51am UTC](https://forums.kodeco.com/t/how-do-i-copy-an-array-within-and-object/38951/3 "2018-01-29T02:51:30Z")

</div>

@jport1130. Thanks very much for your question! I found the following [link](https://www.hackingwithswift.com/example-code/system/how-to-copy-objects-in-swift-using-copy). So in your case, I would perhaps try to make your array conform to the NSCopying protocol, and continue with the steps outlined in the link above.

@jayantvarma I apologize in advance, as I’m not trying to step on your toes here.

I hope this helps!

All the best!

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [July 13, 2018, 10:09pm UTC](https://forums.kodeco.com/t/how-do-i-copy-an-array-within-and-object/38951/4 "2018-07-13T22:09:49Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
