O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required