"Variadic enum cases are not supported" swift 5个错误
"Variadic enum cases are not supported" swift 5 error
我在以下代码中遇到 Variadic enum cases are not supported
错误。这在 Swift4 中编译和工作正常,但在 Swift5 中给出编译时错误,Xcode 10.2
enum ModelRule {
case required(keys: String...) // error: Variadic enum cases are not supported
case nonEmptyString(keys: String...) // error: Variadic enum cases are not supported
case emptyString(key: String)
}
虽然消息很清楚,但我想知道他们为什么要删除完美运行的功能?或者我在这里遗漏了什么?
此外,对于上述错误,有没有比以下更好的解决方案?
case required(keys: [String])
可变参数曾经在 swift 4 上起作用,但这不是故意的。
只需使用数组
enum ModelRule {
case required(keys: [String])
case nonEmptyString(keys: [String])
case emptyString(key: String)
}
展开评论:
您应该在案例的关联值中有数组。但是为了方便创建带有可变参数的方法。
示例:
enum ModelRule {
case required(keys: [String])
case nonEmptyString(keys: [String])
case emptyString(key: String)
init(required keys: String...) { // It could be static func, but init here works and looks better
self = .required(keys: keys)
}
init(nonEmptyString keys: String...) {
self = .nonEmptyString(keys: keys)
}
init(emptyString key: String) { // Added this just for my OCD, not actually required
self = .emptyString(key: key)
}
}
用法:
let required = ModelRule(required: "a", "b", "c") // .required(keys: ["a", "b", "c"])
let nonEmpty = ModelRule(nonEmptyString: "d", "e", "f") // .nonEmptyString(keys: ["d", "e", "f"])
let empty = ModelRule(emptyString: "g") // .emptyString(key: "g")
您可以使用静态函数来模拟带有可变参数的情况:
enum ModelRule {
case required(keys: [String])
case nonEmptyString(keys: [String])
case emptyString(key: String)
static func required(keys: String...) -> Self {
.required(keys: keys)
}
static func nonEmptyString(keys: String...) -> Self {
.nonEmptyString(keys: keys)
}
}
用法:
ruleChecker.apply(rule: .required(keys: "a", "b", "c"))
我在以下代码中遇到 Variadic enum cases are not supported
错误。这在 Swift4 中编译和工作正常,但在 Swift5 中给出编译时错误,Xcode 10.2
enum ModelRule {
case required(keys: String...) // error: Variadic enum cases are not supported
case nonEmptyString(keys: String...) // error: Variadic enum cases are not supported
case emptyString(key: String)
}
虽然消息很清楚,但我想知道他们为什么要删除完美运行的功能?或者我在这里遗漏了什么?
此外,对于上述错误,有没有比以下更好的解决方案?
case required(keys: [String])
可变参数曾经在 swift 4 上起作用,但这不是故意的。 只需使用数组
enum ModelRule {
case required(keys: [String])
case nonEmptyString(keys: [String])
case emptyString(key: String)
}
展开评论:
您应该在案例的关联值中有数组。但是为了方便创建带有可变参数的方法。
示例:
enum ModelRule {
case required(keys: [String])
case nonEmptyString(keys: [String])
case emptyString(key: String)
init(required keys: String...) { // It could be static func, but init here works and looks better
self = .required(keys: keys)
}
init(nonEmptyString keys: String...) {
self = .nonEmptyString(keys: keys)
}
init(emptyString key: String) { // Added this just for my OCD, not actually required
self = .emptyString(key: key)
}
}
用法:
let required = ModelRule(required: "a", "b", "c") // .required(keys: ["a", "b", "c"])
let nonEmpty = ModelRule(nonEmptyString: "d", "e", "f") // .nonEmptyString(keys: ["d", "e", "f"])
let empty = ModelRule(emptyString: "g") // .emptyString(key: "g")
您可以使用静态函数来模拟带有可变参数的情况:
enum ModelRule {
case required(keys: [String])
case nonEmptyString(keys: [String])
case emptyString(key: String)
static func required(keys: String...) -> Self {
.required(keys: keys)
}
static func nonEmptyString(keys: String...) -> Self {
.nonEmptyString(keys: keys)
}
}
用法:
ruleChecker.apply(rule: .required(keys: "a", "b", "c"))