Skip to content

Instantly share code, notes, and snippets.

@jeremiegirault
Last active May 23, 2017 10:22
Show Gist options
  • Save jeremiegirault/035300930701ca002b48cc9899db45f6 to your computer and use it in GitHub Desktop.
Save jeremiegirault/035300930701ca002b48cc9899db45f6 to your computer and use it in GitHub Desktop.
Ensure that the consumer of your api calls your callback
final class AssertComplete {
var defused: Bool = false
let function: String
init(function: String = #function) {
self.function = function
}
func defuse() { defused = true }
deinit {
assert(defused, "You must call complete when calling \(function)")
}
}
func delegateActionAndComplete(action: (() -> Void) -> Void) {
let assertComplete = AssertComplete()
action {
assertComplete.defuse()
// when complete
}
}
delegateActionAndComplete { complete in
complete() // do not forget to call complete or else, assert will trigger !
}
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment