Testing Swift’s ErrorType
Further investigation via LLDB reveals that the protocol has some hidden requirements.
(lldb) type lookup ErrorType protocol ErrorType { var _domain: Swift.String { get } var _code: Swift.Int { get } }It becomes clear how
NSError
fulfills this definition: it has these properties, backed by ivars, which Swift can access without dynamic dispatch. What is still unclear is how Swift’s first class enums can automatically fulfill this protocol. Perhaps there is still some magic involved?[…]
Sure, you could catch it as any generic error and then manually check the fields
_domain
and_code
, but there are more elegant alternatives.