类型不符合协议CustomStringConvertible

use*_*482 3 enums swift swift3

我正在实现try catch枚举:

enum processError: Error, CustomStringConvertible {

        case one
        var localizedDescription: String{
            return "one"
        }
        case two
        var localizedDescription: String {
            return "two"
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

type processError不符合协议CustomStringConvertible

但是,如果我在第二种情况下更改变量的名称,我不会收到错误:

enum processError: Error, CustomStringConvertible {

    case one
    var localizedDescription: String{
        return "one"
    }
    case two
    var description: String {
        return "two"
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么我不能为所有情况使用相同的变量名称?

我真的很感谢你的帮助.

rma*_*ddy 6

问题是该CustomStringConvertible协议需要一个属性:

var description: String
Run Code Online (Sandbox Code Playgroud)

您需要拥有该description属性,否则您将收到不符合协议的错误.

我也建议这种方法:

enum processError: Error, CustomStringConvertible {
    case one
    case two

    var description: String {
        switch self {
            case .one:
                return "one"
            case .two:
                return "two"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)