Chapter    32

Using Instances

In the previous two chapters, you saw how to define classes and structures. Structures and classes are used by creating instances (see Listing 32-1).

Listing 32-1. Class and Structure Instances

struct S {    var i = 1}class C {    var i = 1}var s = S()var c = C()

As you can see in Listing 32-1, these constructs are nearly identical, with the exception of the class and struct keywords. You use dot syntax to access properties in the same way with both class and structure instances (see Listing 32-2).

Listing 32-2. Accessing Instance Properties

println("s.i = \(s.i)")println("c.i = \(c.i)")

The statements in Listing 32-2 will print this:

s.i = 1c.i = 1

Reference vs. Copy

You will see the major difference between referencing ...

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.