Skip to content

Commit 594efcb

Browse files
committed
🎶 shake shake shake
0 parents  commit 594efcb

File tree

5 files changed

+364
-0
lines changed

5 files changed

+364
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

ShakeItOff.playground/Contents.swift

Lines changed: 353 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
import Foundation
2+
3+
4+
// Hello!
5+
print("Hello Wisconsin!")
6+
7+
8+
// Variables
9+
var bae = "Calvin Harris"
10+
print("\(bae) is super cute.")
11+
bae = "Tom Hiddleston"
12+
print("\(bae) is much cuter.")
13+
14+
// Constants
15+
let bff = "Karlie"
16+
print("My bff \(bff) is the best.")
17+
18+
// Specifying a variable type
19+
var catCount: Int = 2
20+
print("I currently have \(catCount) cats.")
21+
22+
23+
// Emoji and characters
24+
let kitties = "🐱🐈😸😻"
25+
print(kitties)
26+
// 🐱🐈😸😻
27+
28+
let title = "🎵 Shake it off"
29+
print("Title has \(title.characters.count) chars")
30+
// Title has 14 chars
31+
32+
let 🐱 = "Meredith Gray"
33+
// Singletons: API.☁️.GET()
34+
35+
36+
// Optionals
37+
var currentHit: String? = nil
38+
currentHit = "Shake it off"
39+
40+
print("Current hit is: \(currentHit)")
41+
// Current hit is: Optional("Shake it off")
42+
43+
// Forced unwrapping
44+
print("Current hit is: \(currentHit!)")
45+
// Current hit is: Shake it off
46+
47+
48+
// Optional chaining
49+
func playHit(currentHit: String?) {
50+
if let hit = currentHit {
51+
print("🎶 \(hit) 🎶")
52+
} else {
53+
print("No hit right now.")
54+
}
55+
}
56+
57+
playHit(currentHit: nil)
58+
// No hit right now.
59+
60+
playHit(currentHit: "Shake it off")
61+
// 🎶 Shake it off 🎶
62+
63+
64+
// Optional chaining
65+
func playHit(currentHit: String?, notes: String?) {
66+
if let hit = currentHit, let notes = notes {
67+
print("\(notes) \(hit) \(notes)")
68+
}
69+
}
70+
71+
playHit(currentHit: "Blank Space", notes: "🎵")
72+
// 🎵 Blank Space 🎵
73+
74+
playHit(currentHit: "Bad Blood", notes: "☠️")
75+
// ☠️ Bad Blood ☠️
76+
77+
78+
// Optional chaining
79+
func playHitLouder(currentHit: String?) {
80+
if let loudHit = currentHit?.uppercased() {
81+
print("🔊🎶 \(loudHit) 🔊🎶")
82+
}
83+
}
84+
85+
playHitLouder(currentHit: "We are never ever getting back together")
86+
// 🔊🎶 WE ARE NEVER EVER GETTING BACK TOGETHER 🔊🎶
87+
88+
89+
// the _
90+
var cat: String? = "Olivia"
91+
if let _ = cat {
92+
print("🐱 meow meow!")
93+
}
94+
95+
96+
// Array
97+
let awards: [String] = [
98+
"Academy of Country Music",
99+
"AMA",
100+
"Academy of Country Music",
101+
"Grammy",
102+
"Billboard",
103+
"MTV",
104+
"Grammy",
105+
]
106+
// ... obviously not a complete list
107+
108+
// Set
109+
let exes: Set<String> = [
110+
"Joe Jonas",
111+
"Taylor Lautner",
112+
"Jake Gyllenhaal",
113+
"John Mayer",
114+
"Harry Styles",
115+
"Calvin Harris"
116+
]
117+
// ... obviously not a complete set
118+
119+
// Dictionary
120+
var squad: [String: String] = [
121+
"Karlie Kloss": "model",
122+
"Selena Gomez": "actress",
123+
"Gigi Hadid": "model",
124+
"Lena Dunham": "director",
125+
"Hailee Steinfeld": "actress",
126+
"Ed Sheeran": "musician"
127+
]
128+
// ... obviously not a complete dictionary
129+
130+
131+
// Loops
132+
for ex in exes {
133+
// Lame in no particular order
134+
print("My ex \(ex) is lame.")
135+
}
136+
/* My ex John Mayer is lame.
137+
My ex Calvin Harris is lame.
138+
My ex Joe Jonas is lame.
139+
My ex Harry Styles is lame.
140+
My ex Jake Gyllenhaal is lame.
141+
My ex Taylor Lautner is lame. */
142+
143+
// Looping over a dictionary
144+
for (bff, job) in squad {
145+
print("\(bff) is a \(job).")
146+
}
147+
/* Lena Dunham is a director.
148+
Ed Sheeran is a musician.
149+
Karlie Kloss is a model.
150+
Selena Gomez is a actress.
151+
Gigi Hadid is a model.
152+
Hailee Steinfeld is a actress. */
153+
154+
// isEmpty
155+
if squad.isEmpty {
156+
print("No way, my squad is never empty.")
157+
} else {
158+
print("Party time! 🎉")
159+
}
160+
161+
// Classes
162+
class MediaItem {
163+
var name: String
164+
165+
// Initializer
166+
init(name: String) {
167+
self.name = name
168+
}
169+
170+
// Deinitializer
171+
deinit {
172+
print("Deinitialize!")
173+
}
174+
}
175+
176+
177+
// Subclasses
178+
class Song: MediaItem {
179+
var albumName: String
180+
181+
init(name: String, albumName: String) {
182+
self.albumName = albumName
183+
super.init(name: name)
184+
}
185+
}
186+
187+
class Movie: MediaItem {
188+
var role: String
189+
190+
init(name: String, role: String) {
191+
self.role = role
192+
super.init(name: name)
193+
}
194+
}
195+
196+
// MediaItem array
197+
let works: [MediaItem] = [
198+
Song(name: "Fifteen", albumName: "Fearless"),
199+
Movie(name: "Valentine's Day", role: "Felicia"),
200+
Song(name: "Blank Space", albumName: "1989"),
201+
Song(name: "Shake it Off", albumName: "1989")
202+
]
203+
// Type checking
204+
for work in works {
205+
if work is Song {
206+
print("\(work.name) is a song.")
207+
} else if work is Movie {
208+
print("\(work.name) is a movie.")
209+
}
210+
}
211+
// Fifteen is a song.
212+
// Valentine's Day is a movie.
213+
// Blank Space is a song.
214+
// Shake it Off is a song.
215+
216+
217+
// Type casting
218+
for work in works {
219+
if let song = work as? Song {
220+
// Blank Space is on 1989
221+
print("\(song.name) is on \(song.albumName)")
222+
} else if let movie = work as? Movie {
223+
// Felicia in Valentine's Day
224+
print("\(movie.role) in \(movie.name)")
225+
}
226+
}
227+
// Fifteen is on Fearless
228+
// Felicia in Valentine's Day
229+
// Blank Space is on 1989
230+
// Shake it Off is on 1989
231+
232+
233+
// Enumerations
234+
enum DanceStyle {
235+
case ballet
236+
case hiphop
237+
case jazz
238+
case cheerleader
239+
case robot(name: String)
240+
}
241+
242+
243+
// Switch
244+
print("My dancing is...")
245+
246+
var currentDancing = DanceStyle.ballet
247+
248+
switch currentDancing {
249+
case DanceStyle.ballet:
250+
print("graceful")
251+
case DanceStyle.hiphop:
252+
print("dope")
253+
case DanceStyle.jazz:
254+
print("cool")
255+
case DanceStyle.cheerleader:
256+
print("rah rah")
257+
case DanceStyle.robot:
258+
print("bleep bloop")
259+
}
260+
// My dancing is...
261+
// graceful
262+
263+
// Switch...
264+
// break, variables, fallthrough, default
265+
currentDancing = DanceStyle.robot(name:"Dave")
266+
switch currentDancing {
267+
case DanceStyle.jazz:
268+
print("boop boop boop.")
269+
case DanceStyle.ballet:
270+
break // do nothing
271+
case DanceStyle.robot(let name):
272+
print("Hello, \(name).")
273+
case DanceStyle.cheerleader:
274+
print("Won't you come over here baby, we can ")
275+
fallthrough
276+
default:
277+
print("shake shake shake.")
278+
}
279+
// Hello, Dave.
280+
281+
282+
// Guard
283+
func beachTime(bae: String?) {
284+
guard let bae = bae else {
285+
print("No pics needed.")
286+
return
287+
}
288+
print("Take Instagram with my bae \(bae)!")
289+
}
290+
beachTime(bae: nil)
291+
// No pics needed.
292+
beachTime(bae: "Tom")
293+
// Take Instagram with my bae Tom!
294+
295+
296+
// Error handling
297+
enum Problem: ErrorProtocol {
298+
case badBlood
299+
case bulletHoles
300+
case deepCut
301+
}
302+
303+
func madLove() throws {
304+
throw Problem.badBlood
305+
}
306+
307+
do {
308+
try madLove()
309+
} catch Problem.badBlood {
310+
print("We've got bad blood!")
311+
} catch {
312+
print("We've got a problem.")
313+
}
314+
315+
316+
// Ternary operator
317+
let grammyCount = 29
318+
let plural = grammyCount == 1 ? "" : "s"
319+
print("I won \(grammyCount) Grammy\(plural)!")
320+
// I won 29 Grammys!
321+
322+
323+
// Nil coalescing operator
324+
let defaultHairColor: String = "blond"
325+
326+
var dyedHairColor: String? = nil
327+
var hairColor = dyedHairColor ?? defaultHairColor
328+
print("My hair is currently \(hairColor).")
329+
// My hair is currently blond.
330+
331+
dyedHairColor = "bleach blond"
332+
hairColor = dyedHairColor ?? defaultHairColor
333+
print("My hair is currently \(hairColor).")
334+
// My hair is currently bleach blond.
335+
336+
337+
// Extensions
338+
extension Date {
339+
// Before Taylor Swift
340+
func isBTS() -> Bool {
341+
let year = Calendar.current.component(.year, from: self)
342+
return year < 1989
343+
}
344+
}
345+
346+
let now = Date()
347+
if now.isBTS() {
348+
print("😭")
349+
} else {
350+
print("😀")
351+
}
352+
// 😀
353+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

ShakeItOff.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)