Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type does not conform to protocol CustomStringConvertible

I'm implementing try catch enum:

enum processError: Error, CustomStringConvertible {

        case one
        var localizedDescription: String{
            return "one"
        }
        case two
        var localizedDescription: String {
            return "two"
        }
    }

But I'm getting the following error:

type processError does not conform to protocol CustomStringConvertible

But if I change the name of the variable in the second case I don't get the error:

enum processError: Error, CustomStringConvertible {

    case one
    var localizedDescription: String{
        return "one"
    }
    case two
    var description: String {
        return "two"
    }
}

My question is why I can not have the same name of the variable for all the cases?

I'll really appreciate your help.

like image 786
user2924482 Avatar asked May 05 '26 16:05

user2924482


1 Answers

The issue is that the CustomStringConvertible protocol requires one property:

var description: String

You need to have the description property or you will get the error that it doesn't conform to the protocol.

I also suggest this approach:

enum processError: Error, CustomStringConvertible {
    case one
    case two

    var description: String {
        switch self {
            case .one:
                return "one"
            case .two:
                return "two"
        }
    }
}
like image 172
rmaddy Avatar answered May 07 '26 10:05

rmaddy