swift - Change Error localizedDescription -


this question has answer here:

i've error class is:

public enum modelerror: error {   case invalidarray(model: string)    var localizeddescription: string {     switch self {     case .invalidarray(model: let model):       return "\(model) has invalid array"     default:       return "modelerror"     }   } } 

and when passed error in callback function, want access custom localizeddescription. instance:

func report(_ error: error) {   print("error report: \(error.localizeddescription)") } 

but calling report(modelerror.invalidarray(model: "test")) prints:

"the operation couldn’t completed. (modelerror error 0.)" 

such things seems feasible nserror since can override localizeddescription property there. don't want use nserror since it's not swift thing , lot of libraries work error.

according documentation, localizeddescription implemented in protocol extension, not in protocol declaration, means there's nothing adhere or override. there type-wide interface enums adhere error.

my way around use wrapper protocol:

protocol localizeddescriptionerror: error {     var localizeddescription: string { } }  public enum modelerror: localizeddescriptionerror {     case invalidarray(model: string)      var localizeddescription: string {         switch self {         case .invalidarray(model: let model):             return "\(model) has invalid array"         default:             return "modelerror"         }     } }  let error: localizeddescriptionerror = modelerror.invalidarray(model: "model") let text = error.localizeddescription // model has invalid array 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -