Reading the following from NSHipster got me wondering about NSError and if something could be set up so that NSError implicitly converts to a custom error type.
I created the following:
protocol NSErrorConvertible {
init(error: NSError)
}
and this
struct CustomError: NSErrorConvertible {
var message: String
var code: String?
init(error: NSError) {
code = String(error.code)
message = error.localizedDescription
}
}
However, I don't see anything that would make implicit conversion happen.
I know such a feature exists in C++ with a one argument constructor. Is there something available like that in Swift using protocols or something else?