O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

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

Reference and Value Types

Structures and classes are far more alike in Swift than they are in most languages. However, there is one major difference in how they operate: classes are reference types; structures, enums, and tuples are value types.

What does it mean to be a value type? For one thing, a value type is always treated as a single value, even if it is composed of several individual values via its properties.

In practical terms, this means that when a value type is assigned or passed as a parameter, a copy is made. The following code demonstrates the effect with the Vector structure:

v​a​r​ ​v​e​c​t​o​r​0​ ​=​ ​V​e​c​t​o​r​(​x​:​ ​0​,​ ​y​:​ ​0​)​ ​ ​ ​ ​ ​ ​ ​ ​v​e​c​t​o​r​0​ ​=​ ​{​x​ ​0​,​ ​y​ ​0​}​ v​a​r​ ​v​e​c​t​o​r​1​ ...

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