There is an updated edition of this book available! View Latest Edition

# 10 Structures Written by Ben Morrow

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

You’ve covered some fundamental building blocks of Swift. With variables, conditionals, strings, functions and collections, you’re ready to conquer the world! Well, almost.

Most programs that perform complex tasks benefit from higher levels of abstraction. In addition to an `Int`, `String` or `Array`, most programs use new types specific to the domain of the task at hand. Keeping track of photos or contacts, for example, demands more than the simple types you’ve seen so far.

This chapter introduces structures, which are the first named type you’ll learn about. Structures are types that can store named properties and define their own behaviors. Like a `String`, `Int` or `Array`, you can define your own structures to create named types to use in your code. By the end of this chapter, you’ll know how to define and use your own structures.

## Introducing structures

Imagine you live in a town called Pizzaville. As you might expect, Pizzaville is known for its amazing pizza. You own the most popular (and fastest!) pizza delivery restaurant in Pizzaville — “Swift Pizza”.

As the owner of a single restaurant, you have a limited delivery area. You want to write a program that calculates if a potential customer is within range for your delivery drivers. The first version of your program might look something like this:

``````let restaurantLocation = (2, 4)
let restaurantRange = 2.5

// Pythagorean Theorem 📐🎓
func distance(from source: (x: Int, y: Int),
to target: (x: Int, y: Int)) -> Double {
let distanceX = Double(source.x - target.x)
let distanceY = Double(source.y - target.y)
return (distanceX * distanceX +
distanceY * distanceY).squareRoot()
}
``````
``````func isInDeliveryRange(location: (x: Int, y: Int)) -> Bool {
let deliveryDistance = distance(from: location,
to: restaurantLocation)
return deliveryDistance < restaurantRange
}
``````

Simple enough, right? `distance(from:to:)` will calculate how far away you are from your pizza. `isInDeliveryRange(location:)` will return `true` only if you’re not too far away.

A successful pizza delivery business may eventually expand to include multiple locations, which adds a minor twist to the deliverable calculator. Replace your existing code with the following:

``````let restaurantLocation = (2, 4)
let restaurantRange = 2.5

let otherRestaurantLocation = (7, 8)
let otherRestaurantRange = 1.5

// Pythagorean Theorem 📐🎓
func distance(from source: (x: Int, y: Int),
to target: (x: Int, y: Int)) -> Double {
let distanceX = Double(source.x - target.x)
let distanceY = Double(source.y - target.y)
return (distanceX * distanceX +
distanceY * distanceY).squareRoot()
}

func isInDeliveryRange(location: (x: Int, y: Int)) -> Bool {
let deliveryDistance =
distance(from: location, to: restaurantLocation)

let secondDeliveryDistance =
distance(from: location, to: otherRestaurantLocation)

return deliveryDistance < restaurantRange ||
secondDeliveryDistance < otherRestaurantRange
}
``````

`isInDeliveryRange(location:)` checks both locations to see if you can get your pizza from either one.

Eventually, the rising number of customers will force the business to expand, and soon it might grow to a total of 10 stores! Then what? Do you keep updating your function to check against all these sets of coordinates and ranges?

You might briefly consider creating an array of x/y coordinate tuples to keep track of your pizza restaurants, but that would be both difficult to read and maintain. Fortunately, Swift has additional tools to help you simplify the problem.

Structures are one of the named types in Swift that allow you to encapsulate related properties and behaviors. You can declare a new type, give it a name, and then use it in your code.

``````struct Location {
let x: Int
let y: Int
}
``````
``````let storeLocation = Location(x: 2, y: 4)
``````
``````struct DeliveryArea {
let center: Location
}

var storeArea = DeliveryArea(center: storeLocation, radius: 4)
``````

### Mini-exercise

Write a structure that represents a pizza order. Include toppings, size and any other option you’d want for a pizza.

## Accessing members

With your `DeliveryArea` defined and an instantiated value in hand, you may be wondering how you can use these values. Just as you have been doing with `Strings`, `Arrays`, and `Dictionaries`, you use dot syntax to access members:

``````print(storeArea.radius) // 4.0
``````
``````print(storeArea.center.x) // 2
``````
``````storeArea.radius = 250
``````
``````let fixedArea = DeliveryArea(center: storeLocation, radius: 4)

// Error: Cannot assign to property
``````

### Mini-exercise

Rewrite `isInDeliveryRange` to use `Location` and `DeliveryArea`.

## Introducing methods

Using some of the capabilities of structures, you could now make a pizza delivery range calculator that looks something like this:

``````let areas = [
DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5),
DeliveryArea(center: Location(x: 9, y: 7), radius: 4.5)
]

func isInDeliveryRange(_ location: Location) -> Bool {
for area in areas {
let distanceToStore =
distance(from: (area.center.x, area.center.y),
to: (location.x, location.y))

return true
}
}
return false
}

let customerLocation1 = Location(x: 8, y: 1)
let customerLocation2 = Location(x: 5, y: 5)

print(isInDeliveryRange(customerLocation1)) // false
print(isInDeliveryRange(customerLocation2)) // true
``````
``````func contains(_ location: Location) -> Bool {
let distanceFromCenter =
distance(from: (center.x, center.y),
to: (location.x, location.y))

}
``````
``````let area = DeliveryArea(center: Location(x: 5, y: 5), radius: 4.5)
let customerLocation = Location(x: 2, y: 2)
area.contains(customerLocation) // true
``````

### Mini-exercises

1. Change `distance(from:to:)` to use `Location` as your parameters instead of x-y tuples.
2. Change `contains(_:)` to call the new `distance(from:to:)` with `Location`.
3. Add a method `overlaps(with:)` on `DeliveryArea` that can tell you if the area overlaps with another area.

## Structures as values

The term value has an important meaning when it comes to structures in Swift, and that’s because structures create what are known as value types.

``````var a = 5
var b = a
print(a) // 5
print(b) // 5

a = 10
print(a) // 10
print(b) // 5
``````
``````var area1 = DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5)
var area2 = area1

``````

## Structures everywhere

You saw how the `Location` struct and a simple `Int` share the same copy-on-assignment behavior. They share the behavior because they are both value types, and both have value semantics.

``````public struct Int : FixedWidthInteger, SignedInteger {
// …
}
``````

## Conforming to a protocol

You may have noticed some unfamiliar parts to the `Int` definition from the Swift standard library above. The types `FixedWidthInteger` and `SignedInteger` appear right after the declaration of `Int`:

``````public struct Int : FixedWidthInteger, SignedInteger {
// …
}
``````
``````public protocol CustomStringConvertible {
/// A textual representation of this instance.
public var description: String { get }
}
``````
``````struct DeliveryArea: CustomStringConvertible {
let center: Location
var description: String {
"""
Area with center: (x: \(center.x), y: \(center.y)),
"""
}

func contains(_ location: Location) -> Bool {
distance(from: center, to: location) < radius
}

func overlaps(with area: DeliveryArea) -> Bool {
distance(from: center, to: area.center) <=
}
}
``````
``````print(area1) // Area with center: (x: 2, y: 4), radius: 4.0
print(area2) // Area with center: (x: 2, y: 4), radius: 2.5
``````

## Challenges

Before moving on, here are some challenges to test your knowledge of structures. It is best if you try to solve them yourself, but solutions are available if you get stuck. These came with the download or are available at the printed book’s source code link listed in the introduction.

### Challenge 1: Fruit tree farm

Imagine you’re at a fruit tree farm and you grow different kinds of fruits: pears, apples, and oranges. After the fruits are picked, a truck brings them in to be processed at the central facility. Since the fruits are all mixed together on the truck, the workers in the central facility have to sort them into the correct inventory container one-by-one.

### Challenge 2: A T-shirt model

Create a T-shirt structure that has size, color and material options. Provide methods to calculate the cost of a shirt based on its attributes.

### Challenge 3: Battleship

Write the engine for a Battleship-like game. If you aren’t familiar with Battleship, see here: http://bit.ly/2nT3JBU

## Key points

• Structures are named types you can define and use in your code.
• Structures are value types, which means their values are copied on assignment.
• You use dot syntax to access the members of named types such as structures.
• Named types can have their own variables and functions, which are called properties and methods.
• Conforming to a protocol requires implementing the properties and methods required by that protocol.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.