Answer the question
In order to leave comments, you need to log in
How to add the same method to several classes at once?
I need to add the same method to several classes at once:
class FilterableTable: UITableViewController { ... }
class FilterableCollection: UICollectionViewController { ... }
extension FilterableTable, FilterableCollection { // я знаю, что эта строка некорректна
func filterItems(){ print('filtered!') }
}
var myTable = FilterableTable()
myTable.filterItems()
var myCollection = FilterableCollection()
myCollection.filterItems()
Answer the question
In order to leave comments, you need to log in
Finally found an option that looks the most correct: the banal use of delegates.
class FilterableTable: UITableViewController {
var filterDelegate: FilterDelegate!
func viewDidLoad(){
filterDelegate = Filter()
}
}
class FilterableCollection: UICollectionViewController {
var filterDelegate: FilterDelegate!
func viewDidLoad(){
filterDelegate = Filter()
}
}
protocol FilterDelegate {
func filterItems()
}
class Filter: FilterDelegate {
func filterItems() {
print("Hooray!")
}
}
You didn't do it right. One of those stories where nails are driven in with a microscope. It turns out that all UIViewControllers will have a filterItems method, about which they will not know anything at all. Correct decision
Announce your protocol
protocol <Name> {
func filterItems()
}
extension <Name> {
func filterItems(){ print('filtered!') }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question