|
Hello! I was taking a look at some code and I was wondering if anyone knew the actual purpose of deep copies. Here's some code that basically allows you to use a class (a reference type) as a value type:
This will allow me to copy
I would get the same print result. At this point, I could also just make this a struct:
I would get the same print result as well. These are just example pieces of code, but all of this is making me wonder when I would actually ever need to use a deep copy method. In this context, it doesn't seem very useful. Any thoughts? As a bonus question, as you just saw the deep copy method is written like this:
However, I could also write it like this:
I could do this, right? Creating a Thanks! :) |
|
In this context it seems for me not very useful, either. For example, if you have an object on which you want to do a lot of complicated and long lasting calculations (for whatever reason) you could do them on a deep copy and the original is untouched. This could be handy when you depend on network calls which usually could take a while. So your original is in a correct state all the time. When all is finished you can just reference the copy to your old variable and all is set and in a correct state. Personally, I wasn't in the situation or had a use case to use a deep copy. But you know the possibility is there and if you need one, you know how to do it. |
|
Sure, but what if you're copying a
This is a rather simplistic case so the above could just as easily be accomplished like so:
but in a real-world case you would almost certainly have much more complicated and numerous properties, including perhaps properties of a data type that has its own properties and so on, and it would be a pain in the butt to have to do that with all of them. A |
|
@roosterboy Are you sure about that first statement? I just tested it now and it's doing what I believed it would do. I have a
After that, I create
When I print the username for both
Please correct me if I'm wrong, but I'm pretty sure I'm right. I would also like to clarify what you mean here:
Perhaps I'm just tired, but I don't really understand your wording here. It would be a pain to instantiate classes compared to using a copy method? Do you mind if you rephrase/elaborate on this? Thanks! :) |
|
You are testing the code you wrote, not the original deep copy code. I was demonstrating that the original deep copy code doesn't set
When you use the original When you use your I mean, with your code, you aren't making a copy of an existing Basically, that
Yeah, but it might not be until tomorrow; I don't have a lot of time to elaborate tonight. |
|
|
|
Let's take the original code, with a
Our output looks something like this:
Now let's take your code, which has a
But our output looks something like this:
Clearly,
Now we have a copy of
This is a very simple example. Just imagine what it would be like if And imagine what this would be like if you had several places in your code where you had to make a copy of a |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.