我一直在努力实现一个Swift数组的扩展以按值删除元素。有人提出使用向下浇注的解决方案,我试图避免由于与动态类型检查相关的成本。你能指出如何获得下面的代码来编译。Swift Array扩展删除元素的值没有转换
感谢,
protocol Removable: SequenceType {
typealias Element
mutating func remove(v: Element) -> Bool
}
func removeElementByValue<T: Equatable>(inout array: [T], valueToRemove: T) -> Bool {
for (index, value) in enumerate(array) {
if value == valueToRemove {
array.removeAtIndex(index)
return true
}
}
return false
}
extension Array: Removable {
mutating func remove(v: T) -> Bool {
return removeElementByValue(&self, v) // compile error here
}
// tried also with:
// mutating func remove<E:Equatable where E == T>(v: T) -> Bool
}
感谢@milos您的回应。我真正希望达到的目的是为了编译。我可以在扩展中移动全局函数,在平等检查时应用投射,并且可以工作。这里是关心的问题,假定协议和函数在给定的情况下如何获得以上编译。因此,我的结论是,除了我缺乏协调跨协议的知识之外,泛型和扩展可能是语言需要更多地指定where子句。 – Atila 2014-10-04 18:51:40