2017-02-28 5 views
1

Array(またはSequenceまたはCollector?)の拡張を定義して、NSIndexPathを使用してカスタムオブジェクトのリストを照会し、indexPathのセクションに基づいてオブジェクトを取得できるようにしたいと行。配列内の配列のSwift汎用拡張子

public var tableViewData = [[MyCellData]]() // Populated elsewhere 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var tableViewCellData = tableViewData.data(from: indexPath) 
    // use tableViewCellData 
} 

// This does not compile as I want the return type to be that of which is the type "in the list in the list" (i.e. MyCellData) 
extension Sequence<T> where Iterator.Element:Sequence, Iterator.Element.Element:T { 
    func object(from indexPath: NSIndexPath) -> T { 
     return self[indexPath.section][indexPath.row] 
    } 
} 

答えて

3
  • Sequenceは添字を経由してインデックスを作成することはできませんので、あなたは Collectionを必要としています。
  • collections要素もコレクションでなければなりません。
  • .row,.sectionIntであるため、コレクション とそのネストされたコレクションのインデックスはIntである必要があります。 は(これは、例えば配列または配列スライス多くのコレクションのためのケースです。 String.CharacterViewないIntでインデックス化 あるコレクションの一例である。)
  • あなたは、任意の一般的なプレースホルダ(およびextension Sequence<T> を必要としません有効なSwift 3構文ではありません)。戻り値の型は、ネストされたコレクションの要素型の として指定してください。

はすべて一緒にそれを置く:

extension Collection where Index == Int, Iterator.Element: Collection, Iterator.Element.Index == Int { 
    func object(from indexPath: IndexPath) -> Iterator.Element.Iterator.Element { 
     return self[indexPath.section][indexPath.row] 
    } 
} 
+0

感謝を!まさに私が探していたもの! – Sunkas