Last active
August 29, 2015 14:07
-
-
Save rydermackay/7ddb1078b0d484da8bc4 to your computer and use it in GitHub Desktop.
Wrapping the NSError ** dance
This file contains 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
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