-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00-tour.swift
70 lines (54 loc) · 1.7 KB
/
00-tour.swift
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This is a comment
// Outputs "Hello, world!"
print("Hello, world!")
var aVariable = 29
let aConstant = "This doesn't need to be known at compile time"
// Swift is statically typed, but it is also type inferred, so isn't necessary
// to always specify the type of a variable or constant
var inferredType = "This is inferred to be a string"
var explicitType: String = "This is explicitly typed as a string"
// Type conversion
print(String(aVariable))
// String interpolation
print("The value of aVariable is \(aVariable)")
// Multi-line strings
var aBigString = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Mattis vulputate enim nulla aliquet
porttitor lacus luctus. Venenatis cras sed felis eget.
"""
// Arrays and dictionaries
var fruits = [ "apples", "oranges", "pears" ]
var occupations = [
"Matt": "Developer",
"Jenny": "Designer",
]
occupations["Josh"] = "Human Resources"
// Arrays grow automatically
fruits.append("bananas")
print(fruits)
// Empty arrays and dictionaries
fruits = []
occupations = [:]
// For empty arrays and dictionaries assignments, is recommended to indicate the
// type
var anEmptyArray: [String] = []
var anEmptyDictionary: [String: Int] = [:]
// ==== Control Flow ====
// If statements
if aVariable > 10 {
print("aVariable is greater than 10")
} else {
print("aVariable is less than 10")
}
// If or Switch statements can be used in assignments
var isTwentyNineGreaterThanTen = if aVariable > 10 {
true
} else {
false
}
// The condition in an If statement must be a boolean expression. Which means
// that code like this won't work:
// if aVariable { ... }
// For loop
var pairs = [ 2, 4, 6, 8 ]