Throughout the past couple of lessons, we've been learning how to use Swift syntax, variables, functions, and control flow structures to develop the building blocks of Swift applications. In the final section of this lesson, we'll learn how to pull all those language components together into Swift's object-oriented classes and structures—the high-level building blocks of most professional Swift applications.

Virtually all object-oriented languages are based on the concept of organizing units of code into classes that perform a very specific set of actions on a specific set of data.

In this lesson, we'll focus on the syntax to define, instantiate, and use your own structs and classes. These techniques are nearly the same for each object type.

A class or struct is defined with the following syntax:

The following are declarations for a Customer object—the first declared as a struct and the second as a class:

Throughout the last couple of lessons, you've been using structs and classes, for example:

As you develop applications with Swift, you'll use classes and structs frequently, and will often define your own.

Next, you'll solidify your understanding of basic struct and class usage by practicing the creation of each type of object in an activity.

To compare the differences (and similarities) between Swift classes and structs, it's useful to implement the same data structure in both. This is exactly what we'll do now.

Use an Xcode playground to practice how to create Swift structs and classes.

  1. Launch Xcode and create a new playground, then save it to your desktop with the name CustomerStructClass.playground.
  2. Add the following lines of code to declare a new Customer struct:
    struct CustomerStruct {
    }
  3. Below the closing brace of the struct definition, create a new variable of type Customer. Congratulations! You've created a struct definition, and instantiated your first custom object!
    var customer1 = CustomerStruct()
  4. Modify the code to the following, adding the enum CustomerType and variable type to the struct. Then modify your code to print the current customer.type to the debug console:
    struct CustomerStruct {
        enum CustomerType: String {
            case gold = "Gold Customer!"
            case silver = "Silver Customer!"
            case unknown = "Unknown customer type"
        }
    
        var type: CustomerType?
    }
    
    var customer1 = CustomerStruct()
    
    print(customer1.type ?? "invalid customer type")
  5. Because this is a struct, Swift has auto-created an initializer we can use to set an initial value for the customer value. Modify the instantiation of the customer variable as follows:
    var customer = CustomerStruct(type: .gold)
    • Now when the code runs, the output is the string gold.
  6. Creating a similar data structure as a class is quite similar. Add the following class definition to your playground:
    class CustomerClass {
        enum CustomerType: String {
            case gold = "Gold Customer!"
            case silver = "Silver Customer!"
            case unknown = "Unknown customer type"
        }
    
        var type: CustomerType?
    
        init(type:CustomerType) {
            self.type = type
        }
    }
  7. Finally, add the following two lines to the playground to instantiate an object of type CustomerClass, and print its type enum member to the debug console:
    var customer2 = CustomerClass(type: .silver)
    print(customer2.type ?? "invalid customer type")