13. let tSwiftAlbums = [
2014 : "1989",
2012: "Red",
2010: "Speak Now",
2008: "Fearless",
2006: "Taylor Swift"]
let possibleAlbumFrom2011: String? = tSwiftAlbums[2011]
let possibleAlbumFrom2014: String? = tSwiftAlbums[2014]
14.
15. 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)")
}
16. 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")
}
20. 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
}
}
24. 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
25. 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
26. "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
29. 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
31. 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")
]
33. class tSwift1989Album {
let title = "1989"
var songs = [Song]()
func add1989Songs() {
for song in tSwiftSongs {
if song.album == "1989" {
songs.append(song)
}
}
}
}
34. let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
35. let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
// MUCH FURTHER DOWN
album.add1989Songs()
36. let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
// MUCH FURTHER DOWN
album.add1989Songs()
album.songs.count // 10
38. /// Return an `Array` containing the elements `x` of `self` for which
/// `includeElement(x)` is `true`
func filter(includeElement: (T) -> Bool) -> [T]
39. class tSwift1989Album {
let title = "1989"
var songs = [Song]()
func add1989SongsWithFilter() {
songs = tSwiftSongs.filter({ song in song.album == "1989"})
}
}