Skip to content

Instantly share code, notes, and snippets.

@zacclark
Last active January 9, 2017 23:42
Show Gist options
  • Save zacclark/bf747624b254dcf0b7c4c6efaf56f0fd to your computer and use it in GitHub Desktop.
Save zacclark/bf747624b254dcf0b7c4c6efaf56f0fd to your computer and use it in GitHub Desktop.
Simple pop operation to simplify a common setup
func pop<T>(
_ maybeValue: inout T?,
_ block: ((T) -> Void)
) {
guard let value = maybeValue else { return }
maybeValue = nil
block(value)
}
class Example {
var someStoredThing : String?
func cleanup() {
if let thing = self.someStoredThing {
print("Got a thing: \(thing)")
self.someStoredThing = nil
}
}
}
class Example {
var someStoredThing : String?
func cleanup() {
pop(&self.someStoredThing) { thing in
print("Got a thing: \(thing)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment