8.
let tSwiftAlbums = [
2014 : "1989",
2012: "Red",
2010: "Speak Now",
2008: "Fearless",
2006: "Taylor Swift"]
let possibleAlbumFrom2011: String? = tSwiftAlbums[2011]
let possibleAlbumFrom2014: String? = tSwiftAlbums[2014]
9.
let tSwiftAlbums = [
2014 : "1989",
2012: "Red",
2010: "Speak Now",
2008: "Fearless",
2006: "Taylor Swift"]
let possibleAlbumFrom2014: String? = tSwiftAlbums[2014]
if possibleAlbumFrom2014 == .None {
println("Taylor Swift had no albums in 2014")
} else {
let albumFrom2014 = possibleAlbumFrom2014!
println("Taylor Swift's 2014 album was (albumFrom2014)")
}
10.
let tSwiftAlbums = [
2014 : "1989",
2012: "Red",
2010: "Speak Now",
2008: "Fearless",
2006: "Taylor Swift"]
if let albumFor2014 = tSwiftAlbums[2014] {
println("Taylor Swift's 2014 album was (albumFor2014)")
} else {
println("Taylor Swift had no albums in 2014")
}
13.
class Album {
let title: String
let artist: String
var copiesSold: Int
init(title: String, artist: String, copiesSold: Int) {
self.title = title
self.artist = artist
self.copiesSold = copiesSold
}
}
17.
Use a value type when:
—Comparing instance data with == makes sense
—You want copies to have independent state
—The data will be used in code across multiple
threads
Swift Blog: Value and Reference Types
18.
Use a reference type (e.g. use a class) when:
—Comparing instance identity with == makes sense
—You want to create shared, mutable state
Swift Blog: Value and Reference Types
19.
"Almost all types in Swift are value
types, including arrays,
dictionaries, numbers, booleans,
tuples, and enums. Classes are the
exception rather than the rule." -
Functional Swift Book
22.
a higher-order function is a function that does at
least one of the following:
—takes one or more functions as an input
—outputs a function
- Wikipedia
24.
struct Song {
let title: String
let album: String
}
let tSwiftSongs = [
Song(title: "Blank Space", album: "1989"),
Song(title: "All You Had to Do Was Stay", album: "Red"),
Song(title: "Back to December", album: "Speak Now"),
Song(title: "All You Had to Do Was Stay", album: "1989"),
Song(title: "Begin Again", album: "Red"),
Song(title: "Clean", album: "1989"),
Song(title: "Love Story", album: "Fearless"),
Song(title: "Shake It Off", album: "1989"),
Song(title: "Bad Blood", album: "1989")
]
25.
struct tSwift1989Album {
let title = "1989"
var songs = [Song]()
}
26.
class tSwift1989Album {
let title = "1989"
var songs = [Song]()
func add1989Songs() {
for song in tSwiftSongs {
if song.album == "1989" {
songs.append(song)
}
}
}
}
27.
let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
28.
let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
// MUCH FURTHER DOWN
album.add1989Songs()
29.
let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
// MUCH FURTHER DOWN
album.add1989Songs()
album.songs.count // 10
31.
/// Return an `Array` containing the elements `x` of `self` for which
/// `includeElement(x)` is `true`
func filter(includeElement: (T) -> Bool) -> [T]
32.
class tSwift1989Album {
let title = "1989"
var songs = [Song]()
func add1989SongsWithFilter() {
songs = tSwiftSongs.filter({ song in song.album == "1989"})
}
}
33.
songs = tSwiftSongs.filter({ song in song.album == "1989"})
34.
songs = tSwiftSongs.filter({ song in song.album == "1989"})
songs = tSwiftSongs.filter({ $0.album == "1989"})