April 2026
Intermediate
683 pages
25h 29m
English
Just like structs, classes can also be used as function parameters. Listing 9.33 demonstrates an example, in which areNameSake accepts two Person instances (objects) to determine if they are namesakes.
class Person { var name: String init(name: String) { self.name = name }}func areNameSake(_ p1: Person, _ p2: Person) -> Bool { return p1.name == p2.name}let student1 = Person(name: "Alice")let student2 = Person(name: "Bob")let student3 = Person(name: "Alice")print(areNameSake(student1, student2)) // falseprint(areNameSake(student1, student3)) // true
Listing 9.33 Classes as Function Parameters
So far, so good: If Person was a struct, you could proceed with the same syntax. However, a logical difference ...
Read now
Unlock full access