【发布时间】:2021-05-09 16:54:42
【问题描述】:
前几天,我问过this question。
绝妙的解决方案是这样的:
enum MyEnum {
case one
case two
case three(user: String)
func isOfSameType(_ other: MyEnum) -> Bool {
switch (self, other) {
case (.one, .one):
return true
case (.two, .two):
return true
case (.three, .three):
return true
default:
return false
}
}
}
var array: [MyEnum] = []
func add(_ value: MyEnum) {
if array.contains(where: { value.isOfSameType($0) }) { return }
array.append(value)
}
现在,冷血分析,这似乎完全是黑魔法。
switch怎么能不带参数比较东西呢?
但现在我需要检查stack 上的最后一个元素是否属于three。
类似
if array!.last.isOfType(.three) {
}
这行不通。 Xcode 将需要来自 .three 的参数。
因为到目前为止我还不了解我所拥有的,所以我不知道该怎么做。
【问题讨论】:
-
对不起,我写错了问题。什么不工作是知道它是否是需要参数的类型,比如.three?我已经解决了这个问题。