Skip to content

Instantly share code, notes, and snippets.

View gallaugher's full-sized avatar

Gallaugher gallaugher

View GitHub Profile
@gallaugher
gallaugher / deleteData in Review.swift
Created June 26, 2018 11:46
Review.swift deleteData
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)
}
@gallaugher
gallaugher / ReviewTableViewControler deleteButtonPressed.swift
Created June 26, 2018 12:00
ReviewTableViewControler.swift deleteButtonPressed
@IBAction func deleteButtonPressed(_ sender: UIButton) {
review.deleteData(spot: spot) { success in
if success {
self.leaveViewController()
} else {
print("😡 Delete unsuccessful.")
}
}
}
@gallaugher
gallaugher / updateAverageReview.swift
Created June 26, 2018 12:18
Spot.swift updateAverageReview
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
@gallaugher
gallaugher / updated deleteData.swift
Created June 26, 2018 12:31
Review.swift updated deleteData
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)
}
@gallaugher
gallaugher / forInForEach.swift
Created June 27, 2018 00:26
Printing for in vs. forEachj
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)}
@gallaugher
gallaugher / Simplifying forEach.swift
Created June 27, 2018 01:21
Simplifying closure code
// 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)
@gallaugher
gallaugher / for in vs .map.swift
Last active June 27, 2018 01:43
for in vs. .map
// 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 }
@gallaugher
gallaugher / forInAverage.swift
Last active June 27, 2018 14:15
Average with a for in loop
var total = 0
for score in scores {
total = total + score
}
let average = Double(total) / Double(scores.count)
@gallaugher
gallaugher / fullSyntaxReduce.swift
Last active June 27, 2018 02:59
full syntax of .reduce used to total an array of Int
// 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)
@gallaugher
gallaugher / reduceForAverageExamples.swift
Created June 27, 2018 03:05
Steps to shrink .reduce code for finding an average of an array of Ints
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: