Skip to content

Instantly share code, notes, and snippets.

@polac24
Last active March 6, 2019 01:59
Show Gist options
  • Save polac24/85262c8a65dde4e8ab7c1b8c1debcd79 to your computer and use it in GitHub Desktop.
Save polac24/85262c8a65dde4e8ab7c1b8c1debcd79 to your computer and use it in GitHub Desktop.
func weaklyScoped<T: AnyObject>(_ v: @autoclosure () -> T, action: (T) throws -> ()) rethrows -> T? {
weak var weakValue: T?
do { // optionally autoreleasepool
let value = v()
try action(value)
weakValue = value
}
return weakValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment