-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
49 lines (38 loc) · 1.01 KB
/
Contents.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
import UIKit
// Exercise - Scope
for i in 0..<10 {
var foo = 55
print("The value of foo is \(foo)")
}
//print("The value of foo is \(foo)")
// The above print statement will produce a complie error as var foo's scope only extends to the end of the for loop.
// ------
var x = 10
for i in 0..<10 {
var foo = 55
x += 1
print("The value of x is \(x)")
}
print("The final value of x is \(x)")
// The above pront statements will complie as var x is declated in the global scope and therefore accessable to all functions.
// ------
func greeting(greeting: String?, name: String) {
if let greeting = greeting {
print("\(greeting), \(name)")
} else {
print("Hello \(name)")
}
}
greeting(greeting: "Hi there", name: "Sara")
greeting(greeting: nil, name: "Sara")
// ------
class Car {
let make: String
let model: String
let year: Int
init(make: String, model: String, year: Int) {
self.make = make
self.model = model
self.year = year
}
}