Skip to content

Instantly share code, notes, and snippets.

@rydermackay
Last active August 29, 2015 14:07
Show Gist options
  • Save rydermackay/7ddb1078b0d484da8bc4 to your computer and use it in GitHub Desktop.
Save rydermackay/7ddb1078b0d484da8bc4 to your computer and use it in GitHub Desktop.
Wrapping the NSError ** dance
final class Box<T> {
let value: T
init(_ v: T) {
value = v
}
}
enum Result<T> {
case Success(Box<T>)
case Error(NSError)
}
func evaluateCocoaStatementWrappingError<T>(statement: (errorPointer: NSErrorPointer) -> T?) -> Result<T> {
var error: NSError?
if let result = statement(errorPointer: &error) {
return .Success(Box(result))
} else {
return .Error(error!)
}
}
let result = evaluateCocoaStatementWrappingError { url.bookmarkDataWithOptions(NSURLBookmarkCreationOptions.WithSecurityScope, includingResourceValuesForKeys: nil, relativeToURL: nil, error: $0) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment