-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathvalues-and-references.swift
More file actions
35 lines (25 loc) · 901 Bytes
/
values-and-references.swift
File metadata and controls
35 lines (25 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// # Classes are reference types
class Job {
var title = ""
}
var job1 = Job()
var job2 = job1
job1.title = "singer"
print(job2.title) // singer
// Triple equals checks identity.
print(job1 === job2) // true
// # Dictionaries are copied at the point of assignment
var band1 = ["bob": "singer", "dan": "guitarist"]
var band2 = band1
band2["bob"] = "drummer"
print(band1["bob"]) // singer
// # Arrays, Strings, and Dictionaries
// Arrays, Strings, and Dictionaries are implemented as structs,
// so they're copied when they're assigned to a new constant or
// variable, or when they'e passed to a function / method
var bands1 = ["radiohead", "telekinesis", "nada surf"]
var bands2 = bands1
print(bands1 == bands2) // true
bands1[0] = "the orwells"
// Now `bands2` is a separate copy.
print(bands2[0]) // radiohead