Casting CustomError for ErrorType for NSError loses userInfo

Attempting to match types CustomErrorwith compatible ones (ErrorType, NSError) leads to the loss of a dictionary of user information:

class CustomError: NSError {}

let error = CustomError(domain: "com.customerrorexample", code: 500, userInfo: [NSLocalizedDescriptionKey: "A great description"])

then

((error as ErrorType) as NSError).localizedDescription // "The operation couldn't be completed..."

However, this will print the correct description:

((error as ErrorType) as! CustomError).localizedDescription // "A great description"

How is it that the ((CustomError as ErrorType) as NSError)userInfo dictionary is losing? How can I get around this, knowing that my actual code will accept the input ErrorTypeand print it localizedDescription- which should be accurate regardless of the NSError subclass?

Edit

See my own answer here: stack overflow . Still not the best solution, feel free to offer the best.

+4
source share
1

, , ErrorType NSError, :

((error as Any) as! NSError).localizedDescription // "A great description"
+6

Source: https://habr.com/ru/post/1607329/


All Articles