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]
}
}
謝謝!正是我在找什麼! – Sunkas