-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
Before we explore the capabilities of the Mirror API, we will need to create a type that can be used for the examples. Let's create the following Person structure:
struct Person {
var firstName: String
var lastName: String
var age: Int
}
In this structure we define three properties, firstName and lastName both of the string type and age which is an integer type. Now let's see how we would use the Mirror API with our this type.
let person = Person(firstName: "Jon", lastName: "Hoffman", age: 55)
let mirror = Mirror(reflecting: person)
This code starts off by creating an instance of the Person type. We then create an instance of the Mirror type using the Person instance. Now we can use the mirror instance to access different attributes in order to inspect the person instance.
Let's see how we can use the displayStyle and subjectType properties using the mirror instance. The following code will display...