Complete the code to declare an unowned reference to avoid retain cycles.
class Person { var name: String init(name: String) { self.name = name } } class Apartment { unowned var tenant: [1] init(tenant: Person) { self.tenant = tenant } }
The unowned reference must be of the class type it refers to, here Person.
Complete the code to declare an unowned reference correctly.
class Customer { var name: String init(name: String) { self.name = name } } class CreditCard { unowned [1] customer: Customer init(customer: Customer) { self.customer = customer } }
var instead of let for unowned reference.An unowned reference must be declared with let because it cannot be nil after initialization.
Fix the error in the code by choosing the correct keyword for the reference to avoid a runtime crash.
class Owner { var name: String init(name: String) { self.name = name } } class House { [1] var owner: Owner? init(owner: Owner) { self.owner = owner } }
Since owner is optional and can become nil, it should be a weak reference to avoid crashes.
Fill both blanks to create a class with an unowned reference and initialize it properly.
class Driver { var name: String init(name: String) { self.name = name } } class Car { [1] var driver: Driver init(driver: [2]) { self.driver = driver } }
The driver property is an unowned reference of type Driver.
Fill all three blanks to create a pair of classes with unowned references to avoid retain cycles.
class Teacher { var name: String init(name: String) { self.name = name } } class Classroom { [1] var teacher: Teacher init(teacher: [2]) { self.teacher = teacher } func assignTeacher() -> [3] { return teacher.name } }
The teacher property is an unowned reference of type Teacher, and the function returns a String.