[swift-users] Confusing Error localizedDescription output

Jon Shier jon at jonshier.com
Tue Feb 28 14:54:36 CST 2017


Swifters:
	I have a custom error enum:

enum MyError: Error {
    
    case network(error: Error)
    case json(error: Error)
    case value(error: Error)
    
    var localizedDescription: String {
        switch self {
        case let .network(error): return error.localizedDescription
        case let .json(error): return error.localizedDescription
        case let .value(error): return error.localizedDescription
        }
    }
    
}

However, the localizedDescription output is odd to me. When the value is treated as:

Error:  po error.localizedDescription
"The operation couldn’t be completed. (MyApp.MyError error 1.)”

LocalizedError:  po (error as! LocalizedError).localizedDescription
"The operation couldn’t be completed. (MyApp.MyError error 1.)”

MyError: Error:  po (error as! MyError).localizedDescription
"JSON could not be serialized because of error:\nThe data couldn’t be read because it isn’t in the correct format."

Shouldn’t my implementation of localizedDescription take precedence in all cases here? (This is Xcode 8.3b3).



Jon


More information about the swift-users mailing list