April 2017
Intermediate to advanced
316 pages
9h 33m
English
In real-world problems, we may need to mix reference types with value types. For instance, we may need to have a reference to class in struct like our previous example or we may need to have a struct variable in class. How would we reason about the assignments and copying in these circumstances?
Let's examine the following example:
class User { var name: String init(name: String) { self.name = name } } let julie = User(name: "Julie") struct Student { var user: User } let student = Student(user: julie) student.user.name // prints "Julie" let anotherStudent = student julie.name = "Julie Jr." anotherStudent.user.name // prints "Julie Jr."
In this example, we have a User class and a Student struct that ...
Read now
Unlock full access