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.

Copying value types

Copying value types - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Copying value types

- [Instructor] Value types get automatically copied upon assignment. Let's switch to X code to prove it. I've gone ahead and created a Swift playground project called Prototype Demo. Next, we're going to define a basic structure. Let's call it NameStruct. I add two properties of type String, firstName and lastName. Note that String is a value type. As you can see, it is declared as a structure. Next, I create a variable called joe, and assign it a NameStruct instance. firstName, let's call him Joe, and lastName Satriani. And now, let's create another variable called patrick, and assign the first instance to it. Finally, print the description of both instances to the console. Both joe and patrick. Let's hit run. The console shows the following. You've got NameStruct with the first name Joe and the last name Satriani, and the second one is also the same. The clone and the original instance share the same values for firstName and lastName. However, NameStruct is a value type, so the…

Contents