-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftExamples.swift
103 lines (78 loc) · 1.87 KB
/
SwiftExamples.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import UIKit
// Note: See SwiftExamples.txt for explainations of this code and some tips.
// Creating variables
let name: String = "Bob"
let hair: CGColor = CGColor(red: 0.70, green: 0.35, blue: 0.0, alpha: 1)
let age: Int = 42
let pets: [String] = []
let dateBorn: Date
let friend: (String, Int) = ("Carl", 41)
// Printing values to the screen
print("This will be printed! ♥")
let sampleString: String = "~ SaMpLe StRiNg ~"
print("Here is a sample string:")
print(" '\(sampleString)' ")
// Sample functions
func myFunction(_ num: Int) -> String {
return "Your number was \(num + 5). Just kidding! It was actually \(num)."
}
func longerFunction(_ n1: Int, _ n2: Int) -> Bool {
return n1 == n2
}
// Loops
for i in 0...10 {
print(i)
}
var counter: Int = 0
while counter <= 10 {
print(counter)
counter += 1
}
// Sample enum
enum Weather {
case rainy
case sunny
case foggy
case cloudy
case snowy
}
// Using if/else
let string = "Lorem ipsum"
if string.first == "L" {
print("string starts with 'L'")
} else if string.count < 5 {
print("string is short")
} else {
print("string is not special :'(")
}
// Using switch case
var person = "Bob"
switch person {
case "Alice":
print("I know you!")
case "Carl", "Dave", "John":
print("I'm sorry, what did you say your name was?")
case "Elaine":
print("Hello, friend!")
default:
print("Sorry, I don't know you.")
}
// Using an enum (and switch case) in a function
func weatherReport(_ weather: Weather) -> String {
switch weather {
case Weather.rainy:
return "🌧️"
case Weather.sunny:
return "☀️"
case Weather.foggy:
return "🌫️"
case Weather.cloudy:
return "☁️"
case Weather.snowy:
return "🌨️"
}
}
// Using sleep()
print("Waiting 3 seconds...")
sleep(3)
print("Finished waiting three seconds!")