我在使用Where子句和Swift时遇到错误,但我不知道如何解决它,代码和错误如下所示。
struct Stack<T>: Container {
// original Stack<T> implementation
var items = T[]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
// conformance to the Container protocol
mutating func append(item: T) {
self.push(item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> T {
return items[i]
}
}
********************************************************************************
func allItemsMatch<C1: Container, C2: Container
where C1.ItemType == C2.ItemType, C1.ItemType: Equatable>
(someContainer: C1, anotherContainer: C2) -> Bool {
// check that both containers contain the same number of items
if someContainer.count != anotherContainer.count {
return false
}
// check each pair of items to see if they are equivalent
for i in 0..someContainer.count {
if someContainer[i] != anotherContainer[i] {
return false
}
}
// all items match, so return true
return true
}
*****************************************************************************************
var stackStrings = Stack<String>()
stackOfStrings.push("uno")
stackOfStrings.push("dos")
stackOfStrings.push("tres")
var arrayOfStrings = ["uno", "dos", "tres"]
//I got the error from the following line of code
//(Swift Compiler Error: Cannot convert the expression's type 'Bool' to type '@lvalue Array')
if allItemsMatch(stackStrings, arrayOfStrings) {
println("All items match.")
} else {
println("Not all items match.")
}
非常感谢任何帮助或建议。