在枚举关联值中不要求外部参数名称

时间:2016-03-22 02:18:15

标签: ios swift

说我有enum

enum TextFieldRule {
    case MinLength(length: Int)
    case MaxLength(length: Int)
    case MatchesOtherTextField(textField: UITextField)
}

可以称为:

.MinLength(length: 1)

有没有办法让外部参数名称不需要?如下所示:

enum TextFieldRule {
    case MinLength(_ length: Int)
    case MaxLength(_ length: Int)
    case MatchesOtherTextField(_ textField: UITextField)
}

并致电:

.MinLength(1)

1 个答案:

答案 0 :(得分:4)

param名称不是必需的

enum TextFieldRule {
    case MinLength(Int)
    case MaxLength(Int)
    case MatchesOtherTextField(UITextField)
}

let rule = TextFieldRule.MinLength(1)

switch rule {
case .MinLength(let length):
    break
case .MaxLength(let length):
    break
case .MatchesOtherTextField(let textField):
    break
}