我想测试两个Swift enum值的相等性。例如:

enum SimpleToken {
    case Name(String)
    case Number(Int)
}
let t1 = SimpleToken.Number(123)
let t2 = SimpleToken.Number(123)
XCTAssert(t1 == t2)

但是,编译器不会编译等式表达式:

error: could not find an overload for '==' that accepts the supplied arguments
    XCTAssert(t1 == t2)
    ^~~~~~~~~~~~~~~~~~~

我需要自己定义重载的相等运算符吗?我希望Swift编译器能自动处理它,就像Scala和Ocaml那样。


当前回答

扩展mbpro的答案,下面是我如何使用该方法在一些边缘情况下检查swift枚举与相关值的相等性。

当然,你也可以使用switch语句,但有时最好只检查一行中的一个值。你可以这样做:

// NOTE: there's only 1 equal (`=`) sign! Not the 2 (`==`) that you're used to for the equality operator
// 2nd NOTE: Your variable must come 2nd in the clause

if case .yourEnumCase(associatedValueIfNeeded) = yourEnumVariable {
  // success
}

如果你想比较同一个If子句中的两个条件,你需要使用逗号而不是&&操作符:

if someOtherCondition, case .yourEnumCase = yourEnumVariable {
  // success
}

其他回答

从Swift 4.1中,只需将Equatable协议添加到您的枚举中,并使用XCTAssert或XCTAssertEqual:

enum SimpleToken : Equatable {
    case Name(String)
    case Number(Int)
}
let t1 = SimpleToken.Number(123)
let t2 = SimpleToken.Number(123)
XCTAssertEqual(t1, t2) // OK

扩展mbpro的答案,下面是我如何使用该方法在一些边缘情况下检查swift枚举与相关值的相等性。

当然,你也可以使用switch语句,但有时最好只检查一行中的一个值。你可以这样做:

// NOTE: there's only 1 equal (`=`) sign! Not the 2 (`==`) that you're used to for the equality operator
// 2nd NOTE: Your variable must come 2nd in the clause

if case .yourEnumCase(associatedValueIfNeeded) = yourEnumVariable {
  // success
}

如果你想比较同一个If子句中的两个条件,你需要使用逗号而不是&&操作符:

if someOtherCondition, case .yourEnumCase = yourEnumVariable {
  // success
}

另一种方法使用if大小写加逗号,在Swift 3中有效:

enum {
  case kindOne(String)
  case kindTwo(NSManagedObjectID)
  case kindThree(Int)

  static func ==(lhs: MyEnumType, rhs: MyEnumType) -> Bool {
    if case .kindOne(let l) = lhs,
        case .kindOne(let r) = rhs {
        return l == r
    }
    if case .kindTwo(let l) = lhs,
        case .kindTwo(let r) = rhs {
        return l == r
    }
    if case .kindThree(let l) = lhs,
        case .kindThree(let r) = rhs {
        return l == r
    }
    return false
  }
}

我是这样写我的项目的。但我不记得我是怎么想到的。(我刚刚谷歌了一下,但没有看到这样的用法。)任何意见都将不胜感激。

t1和t2不是数字,它们是带有关联值的simpletoken实例。

你可以说

var t1 = SimpleToken.Number(123)

你可以说

t1 = SimpleToken.Name(“Smith”) 

没有编译器错误。

要从t1中检索值,使用switch语句:

switch t1 {
    case let .Number(numValue):
        println("Number: \(numValue)")
    case let .Name(strValue):
        println("Name: \(strValue)")
}

在我看来,实现Equatable有点过头了。想象你有一个复杂的大枚举,有很多case和不同的参数。这些参数也都必须实现Equatable。此外,谁说你在全有或全无的基础上比较枚举案例?如果您正在测试值,并且只存根了一个特定的enum参数怎么办?我强烈建议简单的方法,比如:

if case .NotRecognized = error {
    // Success
} else {
    XCTFail("wrong error")
}

…或者对于参数求值:

if case .Unauthorized401(_, let response, _) = networkError {
    XCTAssertEqual(response.statusCode, 401)
} else {
    XCTFail("Unauthorized401 was expected")
}

在这里找到更详细的描述:https://mdcdeveloper.wordpress.com/2016/12/16/unit-testing-swift-enums/