Arrays XCode 7 Beta 3-阵列扩展
XCode 7 Beta 3之前的旧版本代码:Arrays XCode 7 Beta 3-阵列扩展,arrays,swift,xcode7-beta3,Arrays,Swift,Xcode7 Beta3,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(include
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
}
}
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
}
}
“[String]”没有名为“filterByIndex”的成员。
更改了什么?新版本的代码在以下方面对我很好:
[ "zero", "one", "two", "three", "four" ].filterByIndex([1, 3])
// result: [ "one", "three" ]
我想你的问题出在别的地方。起初,我怀疑
重复类型
(其定义未显示)与通用函数的要求不匹配,但在我的测试中,这种情况下的错误消息应该有所不同。我同意@Arkku,代码似乎工作得很好。我想如果你能给出一个例子namesArr
和dupes
,这可能有助于缩小问题的范围(并确保包括这些类型)。let dupes=indiciesofniques(namesArr as![String])
public func indiciesofniques(source:[T])->[Int]{var seen:Set=[]返回source index.filter{if seen.contains(source[$0]){return false}else{seen.insert(source[$0])return true}
我用指示符函数中的元素更改了T,但不起作用…@Bogdanov你试过我答案中的代码了吗?如果这对你有效,那么问题就错了,问题在于重复或名称数组
。要查看是哪一个,请依次用手动创建的数组替换其中的每一个。
[ "zero", "one", "two", "three", "four" ].filterByIndex([1, 3])
// result: [ "one", "three" ]