I have the following class defined on a Playground with Swift 3:
class MyError: Error {
}
Then, I create an instance of such class and check if it is a NSError
let firstError = MyError()
firstError is NSError // Output: false
The output is as expected, and I also get a warning which indicates Cast from 'MyError' to unrelated type 'NSError' always fails
. This makes total sense for me, but if I change the code a little bit and declare the variable as an Error
, I get a strange result:
var secondError: Error
secondError = MyError()
secondError is NSError // Output: true
And in this case I get a warning in the last line that says 'is' test is always true
. I don't get why an Error
would always be an NSError
, when the model is defined the other way round (NSError: Error
). Any idea what is going on here?