3lvis / DATASource

Core Data's NSFetchedResultsController wrapper for UITableView and UICollectionView
Other
106 stars 27 forks source link

Use generics for configuration block #62

Closed 3lvis closed 8 years ago

3lvis commented 8 years ago

Before:

let dataSource = DATASource(tableView: self.tableView, cellIdentifier: CustomCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, configuration: { cell, item, indexPath in
    if let cell = cell as? CustomCell, user = item as? User {
        cell.label.text = user.name
    }
})

After:

let dataSource = DATASource(tableView: self.tableView, cellIdentifier: CustomCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, configuration: { cell, item, indexPath in
    cell.label.text = user.name    
})
3lvis commented 8 years ago

I figured to use generics with UITableViewCell and NSManagedObject I need to initialize the DATASource as a container of this types like this:

DATASource<T: UITableViewCell, U: NSManagedObject>

But is not possible to do such things if you have to inherit from NSObject

DATASource<T: UITableViewCell, U: NSManagedObject>: NSObject

I need to inherit from NSObject because this is implementing a UITableViewDataSource and doing that has the requirement of implementing at least NSObjectProtocol.