Dom*_*ial 7 enumeration swift swift-playground
我使用枚举我遇到一个问题,我无法理解.
这是枚举类型的声明:
enum SomeType {
case un
case deux
case trois
}
Run Code Online (Sandbox Code Playgroud)
然后我想将一个单独的枚举值与一个if语句匹配:
var testValue: SomeType = .trois
if testValue == .trois {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
一切都好!
现在我想只为第一个成员值添加一个关联值:
enum SomeType {
case un(Int)
case deux
case trois
}
var testValue: SomeType = .trois
if testValue == .trois {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
比if声明中出现的错误:Could not find member 'trois'
这是否意味着只能使用switch语句匹配枚举?
精确性
我想要实现的是:"testValue是'trois'的成员价值,不考虑相关价值".换句话说,如何仅在成员值上匹配枚举.
这里实现Airspeed Velocity答案的解决方案:
// Test equality only on member value
func == (lhs:SomeType, rhs:SomeType) -> Bool {
switch (lhs, rhs) {
case (.un(let lhsNum), .un(let rhsNum)):return true
case (.deux, .deux): return true
case (.trois, .trois): return true
default: return false
}
}
// Test equality on member value AND associated value
func === (lhs:SomeType, rhs:SomeType) -> Bool {
switch (lhs, rhs) {
case (.un(let lhsNum), .un(let rhsNum)) where lhsNum == rhsNum: return true
case (.deux, .deux): return true
case (.trois, .trois): return true
default: return false
}
}
var testValue = SomeType.un(3)
// Tests
if testValue == .un(1) {
println("Same member value")
}
if testValue === .un(3) {
println("Same member value AND same associated contents")
}
Run Code Online (Sandbox Code Playgroud)
没有关联类型的枚举是自动等同的.具有关联类型的枚举不是.这是有道理的,因为只有你才能知道应该如何处理相关类型(例如你的.un值附带的整数).即使.trois没有相关类型,缺乏免费赠品等同性也会影响整个枚举.使用模式匹配,Switch的工作方式略有不同,所以它仍然有效.
如果您希望具有相关类型的枚举具有相同性,则可以定义自己的==运算符:
enum SomeType {
case un(Int)
case deux
case trois
}
// possibly there's a more succinct way to do this switch
func ==(lhs: SomeType, rhs: SomeType) -> Bool {
switch (lhs,rhs) {
case let (.un(i), .un(j)) where i == j: return true
case (.deux,.deux): return true
case (.trois, .trois): return true
default: return false
}
}
var testValue: SomeType = .trois
if testValue == .trois {
println("equals .trois")
}
// note, for SomeType to work with generic
// functions that require Equatable, you have
// to add that too:
extension SomeType: Equatable { }
// which means this will work:
let a: [SomeType] = [.un(1), .deux, .trois]
find(a, .trois)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3109 次 |
| 最近记录: |