How to override localizedDescription for custom error in Swift 3?

The error log has only one localizedDescription property . I tried to create a custom object inherited from NSObject and Error, but I cannot override localizedDescription. How can i do this?

This code does not allow me to get a 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) } 

The call function "test_my_code" gets an unexpected result: "The operation could not be completed ...".

What should I do to get the result "MyError: my test string"?

+6
source share
2 answers

The documentation of the new bridge function with the error is not yet clear enough, so in the near future this answer may be needed for some updates, but according to SE-0112 and the latest Swift source code , you may need to use LocalizedError rather than Error and implement errorDescription .

 class MyError: NSObject, LocalizedError { var desc = "" init(str: String) { desc = str } override var description: String { get { return "MyError: \(desc)" } } //You need to implement `errorDescription`, not `localizedDescription`. var errorDescription: String? { get { return self.description } } } func test_my_code() { let error = MyError(str: "my test string") let x = error as Error print(x.localizedDescription) } test_my_code() //->MyError: my test string 

Besides using LocalizedError , this default implementation works:

(NSError.swift, link shown above)

 public extension Error { /// Retrieve the localized description for this error. var localizedDescription: String { return NSError(domain: _domain, code: _code, userInfo: nil).localizedDescription } } 

It's a bit complicated how Swift defines _domain or _code from arbitrary types that only match Error , but it seems that NSError generates "Operation could not be completed ..." for unknown combinations of domain and code.

+22
source

If the custom type conforms to the CustomStringConvertible protocol and provides a localized description , then the following LocalizedError extension may be useful:

 extension LocalizedError where Self: CustomStringConvertible { var errorDescription: String? { return description } } 

Code example:

 class MyError: LocalizedError, CustomStringConvertible { let desc: String init(str: String) { desc = str } var description: String { let format = NSLocalizedString("Operation error: %@", comment: "Error description") return String.localizedStringWithFormat(format, desc) } } let error = MyError(str: "my test string") let x = error as Error print(x.localizedDescription) // Prints "Operation error: my test string" print(String(describing: x)) // Prints "Operation error: my test string" 
+2
source

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


All Articles