Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.4 Value Types vs. Reference Types

Swift’s types are either value types or reference types. In Chapter 2, you worked with variables of type Int—one of the value types. Swift’s numeric types, Bool type and String type are all values types, as are all struct and enum types (which we discuss in later chapters).

Value Types

A value-type constant’s or variable’s value is copied when it’s passed to or returned from a function or method, when it’s assigned to another variable or when it’s used to initialize a constant. Note that Swift’s Strings are value types—in most other object-oriented languages, Strings are reference types.

Reference Types

All class types are reference types. A constant or variable of a reference type (often called a reference) is said to refer to an object. Conceptually this means that the constant or variable stores the object’s location. Unlike Objective-C, C and C++, though, that location is not the actual memory address of the object, rather it’s a handle that enables you to locate the object so you can interact with it. Line 7 of Fig. 3.7:

var formatter = NSNumberFormatter()

creates an object of class NSNumberFormatter, then assigns to the variable formatter a reference to that NSNumberFormatter object. Similarly, line 17 of Fig. 3.9:

let account1 = Account(name: "Jane Green", balance: 50.00)

creates an object of class Account, then initializes the constant account1 with a reference to that Account object.

Objects That Are Assigned to Constants Are Not Constant Objects

Even though account1 and account2 in Fig. 3.9 were defined as constants, this does not make the Account objects themselves constants—assigning an object to a constant simply means that the constant always refers to the same object. You can still use a reference-type constant to access read/write properties and to call methods that modify the referenced object, as we did throughout main.swift in Section 3.3.

Assigning References

Objects of reference types are not copied. If you assign to a reference-type variable another variable or a constant of the same type, then both refer to the same object in memory. The same is true if you initialize a reference-type constant with another variable or a constant of the same type.

Identical to (===) and Not Identical to (!==) Operators

Section 2.7 introduced Swift’s comparative operators. One key difference between value types and reference types is comparing for equality and inequality. Only value-type constants and variables can be compared with == and !=.

You can compare reference-type constants and variables to determine whether they refer to the same object by using the === (identical to) and !== (not identical to) operators. If you have two Account constants (or variables) account1 and account2 that refer to the same Account object, the condition

account1 === account2

returns true. If those constants refer to separate Account objects (even though they might have the same contents), the preceding condition returns false. Similarly, !== returns true if its operands refer to different objects and false if they refer to the same object.

  • + Share This
  • 🔖 Save To Your Account