Complete the code to create a new instance of the class.
class Dog { var name: String init(name: String) { self.name = name } } let myDog = [1](name: "Buddy")
To create a new instance of a class, use the class name followed by parentheses with any required parameters.
Complete the code to change the name property of the Dog instance.
let myDog = Dog(name: "Buddy") myDog.[1] = "Max"
Properties are accessed using their exact name with correct case. Here, 'name' is the property to change.
Fix the error in the code to make both variables refer to the same Dog instance.
let dog1 = Dog(name: "Buddy") let dog2 = [1] dog2.name = "Charlie" print(dog1.name)
Assigning dog2 = dog1 makes both variables point to the same instance, so changes to dog2 affect dog1.
Fill both blanks to complete the class method that changes the dog's name.
class Dog { var name: String init(name: String) { self.name = name } func [1](newName: String) { self.[2] = newName } }
The method name can be 'changeName', and inside it, we assign the newName to the property 'name'.
Fill all three blanks to create a copy of a Dog instance and change the copy's name without affecting the original.
class Dog { var name: String init(name: String) { self.name = name } func copy() -> Dog { return Dog(name: self.[1]) } } let dog1 = Dog(name: "Buddy") let dog2 = dog1.[2]() dog2.[3] = "Max" print(dog1.name)
The copy method returns a new Dog with the same name. Calling dog1.copy() creates a new instance. Changing dog2.name does not affect dog1.