TIP

As you read this lesson and try out the exercises below, you can add to the Data Types and Structures playground that you created in our previous class.

Individual variables or constants only go so far.

What if we want to describe, say, a person?

What attributes does a person have?

These might include:

  • name
  • hair
  • colour
  • age
  • height (in cm)
  • mass (in kg)

Definition of a structure

TIP

Try out each example below in your Xcode Playground to verify the result.

To do this with a code block, hover your mouse over the code block, then click the copy button .

You can then paste the code into your Playground using Command-V.

This is the definition of a structure:

struct Person {
    
    // MARK: Stored properties
    // These are properties that have an assigned value
    let age: Int
    let hairColour: String
    let name: String
    let heightInCentimetres: Double
    let massInKilograms: Double
}

Action

Add the code shown above to your Data Types and Structures playground now.

A structure is a data type, just like String, Int, and Double are data types.

So by defining our own structure, we are literally creating a new data type in Swift!

Creating an instance

How do we use a structure, though?

To create an instance of a type, we must provide values for each property of the type.

Here, two instances of the Person data type are created, as constants:

let joyce = Person(age: 16,
                   hairColour: "black",
                   name: "Joyce",
                   heightInCentimetres: 160,
                   massInKilograms: 30)
 
let gordon = Person(age: 46,
                    hairColour: "red",
                    name: "Gordon",
                    heightInCentimetres: 180,
                    massInKilograms: 78)

Action

Try creating an instance of the Person type named me to represent yourself. Make it a constant, using the let keyword, like the instances of the Person structure shown above.

Once an instance of a structure is created, we can “ask about” the values held in each property, and use these values in programs we author.

Action

Add the following code to the bottom of your Data Types and Structures playground and try it out.

me.age

What shows up in the results sidebar, at right?

Variables and constants

What happens if you try to change, or mutate, one of the properties of the structure?

Action

Add this code at the bottom of the playground:

me.age = 21

Remember, the structure was defined with the age property as a constant. This means the the value of age cannot ever change (cannot ever be mutated):

struct Person {
    
    // MARK: Stored properties
    // These are properties that have an assigned value
    let age: Int           // Defined using "let", so it is constant
    let hairColour: String
    let name: String       
    let heightInCentimetres: Double
    let massInKilograms: Double
}

Action

Change the definition of the name constant so that it is variable – use var instead of let.

Now, try running your playground again – what happens?

We still cannot change the value of the age property because the instance of the Person structure itself was defined as a constant.

Action

Change the code that creates an instance of the Person named me in your playground, so that the instance is a variable, rather than a constant.

To summarize, a structure works like any other data type.

When an instance of a structure is declared as a constant, using the let keyword, that instance cannot be changed.

When an instance of a structure is declared as a variable, using the var keyword, and there are properties of that structure also declared as variables, we can change the values of individual properties.

Discussion

Discuss with a friend in class.

What other properties of the Person structure should be defined as variables?

To answer this question, consider whether the values those properties hold would change over time.

Make any necessary changes to the Person structure in your Xcode playground.

Exercise

Goal: Show you can define a structure, create instances, read properties, and reason about when to use let vs var for properties and for instances.

  1. Pick a domain & define a structure

Choose one domain (or select your own): Book, Animal, VideoGame, Bicycle, Planet, Recipe.

Action

Create a new structure in your Data Types and Structures playground playground. Your struct must:

  • Have 5–7 stored properties with mixed types (String, Int, Double, Bool).
  • Use at least two let properties (values that never change for this thing).
  • Use at least two var properties (values that could change over time).
  • Use clear names and add one-line comments explaining each property’s purpose.

(Think carefully about which attributes are truly fixed vs changeable.)

  1. Create two instances (constants)

Action

Make two instances of your structure using let, with realistic values that are different from each other.

  • Give each instance a short, descriptive constant name.
  • In the results sidebar, expand the instance to verify property values look correct.
  1. Read properties

Action

Add 3–4 single-line expressions that read properties from both instances (for example, paperback.pageCount) and observe the values in the sidebar.

  • Add a brief inline comment for each explaining what is being read and why it makes sense.
  1. Mutability experiments (property vs instance)

You will try to change some values and explain the results.

Action

4a. Attempt to change one of your let properties on one instance.
Predict the outcome in a comment, then run the code and write a one-sentence explanation of the actual error (use the wording Xcode gives you).

Action

4b. Attempt to change one of your var properties on one instance while the instance is a let.
Predict → run → explain the result.

Action

4c. Now create a third instance of your structure using var.
Change a var property on this instance. Predict → run → confirm what happens and explain why this case is different from 4b.

(This section checks your understanding of “property mutability” vs “instance mutability.”)

  1. Mini‑reflection

Action

In 3–4 sentences at the bottom of your playground:

  • Which of your properties were hardest to classify as let vs var, and why?
  • In your own words, explain the difference between:
    1. a property being let vs var, and
    2. an instance being declared with let vs var.

Please add your results for these exercises both as code blocks in Notion (type /code to create one) and by using screenshots of your playground in Xcode.

Solutions

Here is an example solution to the exercises above. This is just one possible set of answers. Please review, comparing these to your own work. Ask questions of Mr. Gordon through your portfolio on Notion using @Russell Gordon as needed. You will see a (somewhat shorter) question like this on Monday’s mini-test. Note that the mini-test on Monday will be written on paper – no computers or other reference material permitted.