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

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