Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
Last active December 27, 2020 02:26
Show Gist options
  • Save SlappyAUS/512d643554c51303c534a0b992d502c3 to your computer and use it in GitHub Desktop.
Save SlappyAUS/512d643554c51303c534a0b992d502c3 to your computer and use it in GitHub Desktop.
Array Filtering #swift #array #filter
// Filter array to only the drinks in the last 24 hours.
private func filterDrinks(drinks: [Drink]) -> [Drink] {
// The current date and time.
let endDate = Date()
// The date and time 24 hours ago.
let startDate = endDate.addingTimeInterval(-24.0 * 60.0 * 60.0)
// return an array of drinks with a date parameter between
// the start and end dates.
return drinks.filter { (drink) -> Bool in
(startDate.compare(drink.date) != .orderedDescending) &&
(endDate.compare(drink.date) != .orderedAscending)
}
}
func removeRows(at offsets: IndexSet) {
numbers.remove(atOffsets: offsets)
}
// Iterate and indexset
indexSet.forEach { islands.remove(at: $0) }
// Filter and Map
private func delete(indexSet: IndexSet) {
let drinkEntries = indexSet
.filter {dm.lastNDrinks[$0].sample != nil }
.map { dm.lastNDrinks[$0].sample! }
dm.deleteSample(samples: drinkEntries) {_,_ in }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment