This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func deleteData(spot: Spot, completed: @escaping (Bool) -> ()) { | |
| let db = Firestore.firestore() | |
| db.collection("spots").document(spot.documentID).collection("reviews").document(documentID).delete() { error in | |
| if let error = error { | |
| print("😡 ERROR: deleting review documentID \(self.documentID) \(error.localizedDescription)") | |
| completed(false) | |
| } else { | |
| spot.updateAverageRating { | |
| completed(true) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func updateAverageRating(completed: @escaping ()->()) { | |
| let db = Firestore.firestore() | |
| let reviewsRef = db.collection("spots").document(self.documentID).collection("reviews") | |
| reviewsRef.getDocuments { (querySnapshot, error) in | |
| guard error == nil else { | |
| print("*** ERROR: failed to get query snapshot of reviews for reviewsRef: \(reviewsRef.path), error: \(error!.localizedDescription)") | |
| return completed() | |
| } | |
| var ratingTotal = 0.0 | |
| for document in querySnapshot!.documents { // go through all of the reviews documents |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func deleteData(spot: Spot, completed: @escaping (Bool) -> ()) { | |
| let db = Firestore.firestore() | |
| db.collection("spots").document(spot.documentID).collection("reviews").document(documentID).delete() { error in | |
| if let error = error { | |
| print("😡 ERROR: deleting review documentID \(self.documentID) \(error.localizedDescription)") | |
| completed(false) | |
| } else { | |
| spot.updateAverageRating { | |
| completed(true) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var scores = [83, 85, 90, 100] | |
| // print all array elements with a for loop: | |
| for score in scores { | |
| print(score) | |
| } | |
| print("^for in ^\n") | |
| // print all elements with forEach: | |
| scores.forEach{print($0)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Examples 1. through 4. below produce the same result and show the path to simplifying down to a single line of Swift code. | |
| // 1. forEach can be written with full closure syntax | |
| scores.forEach({(score: Int) -> () in | |
| print(score) | |
| }) | |
| print("^ 1.\n") | |
| // 2. Swift can infer type from context (score must be an Int since scores is an array of Ints). This lets us remove the ": Int" above. | |
| scores.forEach({(score) -> () in | |
| print(score) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the code below uses a for in loop to create a new array named scoreAfterBonus where every element in scores gets a 10% bonus | |
| var scoresAfterBonus: [Double] = [] | |
| for score in scores { | |
| scoresAfterBonus.append(Double(score) * 1.1) | |
| } | |
| // the code below uses the .map method to do the same thing with just one line. Also note that the array scoresAfterBonus can even be created as a constant if it’s not going to change in the future. Note another array tenPerentScores is created below to demonstrate that it can be created as a constant with let, impossible in the for loop which needs to change the array with each iterration using .append | |
| let tenPercentScores = scores.map { Double($0) * 1.1 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // go through all eleemnts of .scores | |
| // 0 is the initial accumulating value (e.g. start at zero) | |
| // x is the current accumulating value | |
| // y is the next accumulating value | |
| let total = scores.reduce(0, {x, y in | |
| return x + y | |
| }) | |
| let average = Double(total) / Double(scores.count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var scores = [83, 85, 90, 100] | |
| // The full syntax .reduce above does the same thing as this one-liner: | |
| let total = scores.reduce(0, {$0 + $1}) | |
| // Swift even allows us to eliminate the closure, replacing it with an operator and automatically assuming it’ll apply the current accumulating value on the left of the operator, and the next accumulating value to the right | |
| let anotherWayToTotal = scores.reduce(0, +) | |
| // You can even do the entire calculation in a single line, although it starts to get more difficult to read: |