XCode 7 Beta 3 之前的旧版本代码:
extension Array {
func filterByIndex<S: SequenceType where S.Generator.Element == Int>(indices: S) -> [T] {
return Array(PermutationGenerator(elements: self, indices: indices))
}
func find(includedElement: T -> Bool) -> Int? {
for (idx, element) in self.enumerate() {
if includedElement(element) {
return idx
}
}
return nil
}
}
XCode 7 Beta 3 之后的新版本代码:
extension Array {
func filterByIndex<S: SequenceType where S.Generator.Element == Int>(indices: S) -> [Element] {
return Array(PermutationGenerator(elements: self, indices: indices))
}
func find(includedElement: Element -> Bool) -> Int? {
for (idx, element) in self.enumerate() {
if includedElement(element) {
return idx
}
}
return nil
}
}
但是现在,当我编写此行时,函数 filterByIndex 给出了一个错误:
let names = (namesArr as! [String]).filterByIndex(dupes)
“[String]”没有名为“filterByIndex”的成员
改变了什么?
最佳答案
新版本的代码对我来说工作得很好:
[ "zero", "one", "two", "three", "four" ].filterByIndex([1, 3])
// result: [ "one", "three" ]
我假设您遇到的问题是在其他地方。我最初怀疑 dupes 的类型(其定义未显示)与通用函数的要求不匹配,但在我的测试中,在这种情况下错误消息应该不同。
关于arrays - XCode 7 Beta 3 - 数组扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352437/