Last active
January 9, 2017 23:42
-
-
Save zacclark/bf747624b254dcf0b7c4c6efaf56f0fd to your computer and use it in GitHub Desktop.
Simple pop operation to simplify a common setup
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 pop<T>( | |
_ maybeValue: inout T?, | |
_ block: ((T) -> Void) | |
) { | |
guard let value = maybeValue else { return } | |
maybeValue = nil | |
block(value) | |
} |
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
class Example { | |
var someStoredThing : String? | |
func cleanup() { | |
if let thing = self.someStoredThing { | |
print("Got a thing: \(thing)") | |
self.someStoredThing = nil | |
} | |
} | |
} |
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
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