O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mixing value and reference types

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required