Chapter    33

Class Identity Operators

Since class instances are referenced and not copied when you use the assignment operator, it’s common to have many variables in your program referring to the same class instance. This makes it important for you to be able to compare two variables to see whether they point to the same class instance.

Let’s say you are working with the Person class introduced in Chapter 31 (see Listing 33-1).

Listing 33-1. Person Instances

class Person {    var name: String = "Name"    var age:Int = 0    func profile() -> String {        return "I'm \(self.name) and I'm \(self.age) years old."    }}var p1 = Person()p1.name = "Matt"p1.age = 40var p2 = Person()p2.name = "Jill"p2.age = 25

In Listing 33-1, you define a Person ...

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.