Chapter    30

Structures

You can use structures to define custom types in Swift. Structures give you a way of grouping related information together. These Swift constructs give you the same capability as structures in C programming, but as you will see, Swift structures are richer than C structures.

To define a Swift structure, use the struct keyword (see Listing 30-1).

Listing 30-1. Defining Custom Types

struct Rectangle {    var x:Int = 0    var y:Int = 0    var width:Int = 0    var height:Int = 0}

In Listing 30-1, you define a structure named Rectangle. Your rectangle type is made of integers that describe the rectangle, including the x and y origin coordinates and the height and width dimensions.

When used like this in structures and classes ...

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.