I play with protocol extensions and I have a problem. Maybe I want to achieve this. I have this platform:
import UIKit
protocol ArrayContainer {
typealias T
var array: [T] { get }
}
class MyViewController: UIViewController, ArrayContainer, UITableViewDataSource {
typealias T = String
var array = ["I am", "an Array"]
}
extension UITableViewDataSource where Self: ArrayContainer {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
}
This is what I have and what I want:
- I have a protocol
ArrayContainerthat has only types and an array that contains objects of this type typealias - I have a protocol extension
UITableViewDataSourcethat will be used, if the class is consistent with the Protocol ArrayController. This simply returns the number of elements in the array as the number of rows. The method is cellForRowAtIndexPathnot well implemented, but this is not a problem. - I have a subclass
UIViewControllercalled MyViewControllerthat implements both protocols.
, , MyViewController UITableViewDataSource, , , UITableViewDataSource. - ? , , Objective-C ?