From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Pitfalls of cloning reference types

Pitfalls of cloning reference types - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Pitfalls of cloning reference types

- [Instructor] Value types are perfect candidates for the prototype pattern. We get the required behavior for free, without writing a single line of code. Reference types are different. Unlike value types, if we assign the reference type to another object they will point to the same object. Changing any of them will affect the contents of the other one too. If you want to follow along with me, you can find the project in the Exercise Files folder Chapter three, theta three, begin. I add the new playground page called Cloning Reference Types. And let's rename the current one to Cloning Value Types. Let's remove the boilerplate code. This time, I'll declare a class instead of a structure. class. NameClass. And add the same properties, firstName and lastName of type String. Actually, we can go and copy them from here. Since this is a class, I need to add an initializer, too. init(firstName) as the first parameter of type String and lastName. Also of type String. self.firstName equals the…

Contents