Created
August 10, 2016 03:57
-
-
Save prachigauriar/0300599c9baa14ea024202825e8bdd10 to your computer and use it in GitHub Desktop.
ErrorBox for Xcode 8b4 and Xcode 8b5
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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is no longer necessary as of Xcode 8b6.