Swift:Assert语句中的可选元组类型

时间:2015-01-23 13:54:42

标签: swift

我正在尝试在Swift中实现Priority Queue,它仍处于初始阶段。我想首先为数据结构编写测试。

目前我有这个方法extractMin,它返回优先级队列中的第一个元素。它的实现如下(T是通用值类型)。

//  Remove and return the element with minimum priority.
//  If the pq is empty, return nil.
func extractMin() -> (T, Double)? {
    return nil  //  Implementation goes here
}

我想为这个函数编写测试。我要检查的第一件事是当pq为空时它会返回nil。我希望得到类似的东西:

XCTAssertNil(priorityQueue.extractMin(), "A nil value should be returned when the priority queue is empty");

但是,这会提示我一个错误,上面写着“(String,Double)与AnyObject不同”。

有什么方法可以解决这个问题吗?我该如何为这种可选的元组类型编写测试?

1 个答案:

答案 0 :(得分:0)

也许您可以考虑返回两个可选值的元组并检查两者?或者是具有这两个成员的可选类。

此链接还讨论了缺少“导入基础”的可能性:http://www.scottlogic.com/blog/2014/09/24/swift-anyobject.html

另见:How can one use XCTAssertNil with optional structs?