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

Value and reference type constants

Constants behave differently if they are value or reference types. We will be able to change the variables in a constant class, but we cannot change them for structs.

Let's examine the following example:

class User {     var name: String     init(name: String) {         self.name = name     } } let grace = User(name: "Grace") let tamina = User(name: "Tamina") struct Student {     var user: User } let student = Student(user: grace) student.user = tamina // compiler error - cannot assign to property: 'student' is a 'let' constant

In this example, we have a class named User and two constants that point to the instance of the class. Also, we have a Student struct that has a variable of the User type.

We create student using ...

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