Error protocol has only one property localizedDescription. I tried to create custom object inherited from NSObject and Error but I can not override localizedDescription. How can I do that?
This code does not allow me to get custom description:
class MyError: NSObject, Error {
var desc = ""
init(str: String) {
desc = str
}
override var description: String {
get {
return "MyError: \(desc)"
}
}
var localizedDescription: String {
get {
return self.description
}
}
}
func test_my_code() {
let error = MyError(str: "my test string")
let x = error as Error
print(x.localizedDescription)
}
Calling function "test_my_code" get unexpected result: "The operation couldn’t be completed...".
What should I do to get result "MyError: my test string" ?