2015-07-11 64 views
0

舊版本的代碼之前的XCode 7 Beta 3的:的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) 

「[字符串]」沒有一個名爲成員的網絡連接lterByIndex'

什麼變化?

+0

我同意@Arkku,代碼似乎工作得很好。我猜如果你可以給一個'namesArr'和'dupes'的例子,這可能有助於縮小問題的範圍(並確保包括這些類型)。 – justinpawela

回答

0

代碼的新版本工作正常,我用:

[ "zero", "one", "two", "three", "four" ].filterByIndex([1, 3]) 
// result: [ "one", "three" ] 

我假設你遇到的問題是其他地方。我最初懷疑dupes(其定義未顯示)的類型與通用函數的要求不匹配,但在我的測試中,錯誤消息在該情況下應該不同。

+0

'讓愚弄= indicesOfUniques(namesArr如[字符串]!)' '公共FUNC indicesOfUniques (來源:[T]) - > [INT] { 變種看出:設置 = [] 返回source.indices。濾波器{ 如果seen.contains(源[$ 0]){ 返回假 } 否則{ seen.insert(源[$ 0]) 返回真 } } }' 我與元改變Ť indicesOfUniques和不工作... –

+0

@BogdanBogdanov你嘗試從我的答案代碼?如果這對你有用,那麼問題就是錯誤的,問題在於'dupes'或'namesArr'。要查看它們,用手動創建的數組依次替換它們中的每一個。 – Arkku