Skip to content

Instantly share code, notes, and snippets.

@prachigauriar
Created August 10, 2016 03:57
Show Gist options
  • Save prachigauriar/0300599c9baa14ea024202825e8bdd10 to your computer and use it in GitHub Desktop.
Save prachigauriar/0300599c9baa14ea024202825e8bdd10 to your computer and use it in GitHub Desktop.
ErrorBox for Xcode 8b4 and Xcode 8b5
import Foundation
/// Example error
enum MyError : Error {
case error1
}
/// Boxes error in a struct
struct ErrorBox<BoxedError: Error> {
let error: BoxedError
init(_ error: BoxedError) {
self.error = error
}
}
/// Example NSObject subclass containing an error property
class MyClass : NSObject {
// Uncommenting this line will cause a crash
// var error: MyError?
// Workaround is to box the error in an intermediate struct and access the error via the box.
var errorBox: ErrorBox<MyError>?
}
let object = MyClass()
object.errorBox = ErrorBox(.error1)
if let error = object.errorBox?.error {
print(error)
}
@prachigauriar
Copy link
Author

This is no longer necessary as of Xcode 8b6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment