Skip to content

Instantly share code, notes, and snippets.

@jayrhynas
Created June 20, 2018 19:04
Show Gist options
  • Save jayrhynas/64acf5853d928957d6d93eccfa018713 to your computer and use it in GitHub Desktop.
Save jayrhynas/64acf5853d928957d6d93eccfa018713 to your computer and use it in GitHub Desktop.
// traits
protocol TableViewUpdating {
func update(at: IndexPath)
}
extension TableViewUpdating {
func update(at indexPath: IndexPath) {
// default implementation
}
}
protocol TableViewDeleting {
func delete(at: IndexPath)
}
extension TableViewDeleting {
func delete(at indexPath: IndexPath) {
// default implementation
}
}
// classes
class DataSourceOne: UITableViewDataSource, TableViewUpdating, TableViewDeleting {
}
class DataSourceTwo: UITableViewDataSource, TableViewUpdating {
}
// test
let one = DataSourceOne()
one.update(at: IndexPath(item: 0, section: 0))
one.delete(at: IndexPath(item: 0, section: 0))
let two = DataSourceTwo()
two.update(at: IndexPath(item: 0, section: 0))
// two.delete(at: IndexPath(item: 0, section: 0)) => error: value of type 'DataSourceTwo' has no member 'delete'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment