我怎样才能删除警告 "Cannot match several associated values at once"

How can I remove the warning "Cannot match several associated values at once"

我的单元测试中有一个辅助方法:

  func expect(_ sut: CompanyStore, toRetrieve expectedResult: RetrieveCacheResult, when action: @escaping (() -> Void), file: StaticString = #file, line: UInt = #line) {
    let exp = expectation(description: "await completion")

    sut.retrieve { retrievedResult in
      switch (expectedResult, retrievedResult) {
      case (.empty, .empty), (.failure, .failure):
        break
      case let (.found(retrieved), .found(expected)):
        XCTAssertEqual(retrieved.item, expected.item, file: file, line: line)
        XCTAssertEqual(retrieved.timestamp, expected.timestamp, file: file, line: line)

      default:
        XCTFail("Expected to retrieve \(expectedResult), got \(retrievedResult) instead", file: file, line: line)
      }

      exp.fulfill()
    }
    action()
    wait(for: [exp], timeout: 1.0)
  }

它允许我创建测试,例如:

  func test_retrieve_delivers_empty_on_empty_cache() {
    let sut = makeSUT()
    expect(sut, toRetrieve: .empty, when: {
      // some action to perform
    })
  }

自升级到 Swift 5.1 后,我收到以下警告:

Cannot match several associated values at once, implicitly tupling the associated values and trying to match that instead

在下一行

 case let (.found(retrieved), .found(expected)):

这些值是一个元组(item: LocalCompany, timestamp: Date)

我一直无法弄清楚如何清除此警告。

编辑:

.found 是一个枚举:

public enum RetrieveCacheResult {
  case empty
  case found(item: LocalCompany, timestamp: Date)
  case failure(Error)
}

您有两个选择:

  1. 将关联值重新定义为元组。
case found( (item: Int, timestamp: Bool) )
  1. 单独提取所有成员。
case let (
  .found(retrievedItem, retrievedTimestamp),
  .found(expectedItem, expectedTimestamp)
):
  1. 例如你有这个:case todo(shoppingCount: Int, eating: Bool)
  2. 使用中:

           case .todo(let shoppingCount, _):
               print("Shopping count = ", shoppingCount)

           case .todo(_, let eating):
               print("Eating = ", eating)