Generic UITableViewDataSource using Swift

I am trying to create a generic implementation of UITableViewDataSource. Internally, a DataSource implementation uses the NSFetchedResultsController to retrieve data. Here is my current code.

extension UITableViewCell {
    class var reuseIdentifier: String {
        return toString(self).componentsSeparatedByString(".").last!
    }
}

class ManagedDataSource<CellType: UITableViewCell, ItemType>: NSObject, NSFetchedResultsControllerDelegate, UITableViewDataSource {

    // MARK: Properties

    var fetchRequest: NSFetchRequest {
        get {
            return fetchedResultsController.fetchRequest
        }
        set {
            fetchedResultsController = NSFetchedResultsController(fetchRequest: newValue, managedObjectContext: HMServicesManager.mainContext(), sectionNameKeyPath: nil, cacheName: nil)
        }
    }

    private var fetchedResultsController: NSFetchedResultsController {
        didSet {
            fetchedResultsController.delegate = self
        }
    }

    private let configureCell: (item: ItemType, cell: CellType) -> ()
    private weak var tableView: UITableView?

    // MARK: Initialization

    init(fetchRequest: NSFetchRequest,
        tableView: UITableView,
        tableViewCellType cellType: CellType.Type,
        itemType: ItemType.Type,
        configureCell: (item: ItemType, cell: CellType) -> ()) {
            self.tableView = tableView
            self.configureCell = configureCell
            fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: HMServicesManager.mainContext(), sectionNameKeyPath: nil, cacheName: nil)
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CellType.reuseIdentifier, forIndexPath: indexPath) as! CellType
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }


}

The implementation of the UITableViewDataSource and the implementation of NSFetchedResultsControllerDelegate are currently incomplete, but this is not my problem. I get a compiler error in the definition of the ManagedDataSource class:

The type "ManagedDataSource" does not conform to the "UITableViewDataSource" protocol

, , UITableViewDataSource . , , . AnyObject, , ​​ . , , .

+4
1

Swift 2.0 - .

0

All Articles