Trying to conform to UITableViewDataSource and UITableViewDelegate inside a Swift UIViewController subclass.
class GameList: UIViewController { var aTableView:UITableView = UITableView() override func viewDidLoad() { super.viewDidLoad() aTableView.delegate = self aTableView.dataSource = self self.view.addSubview(aTableView) //errors on both lines for not conforming } }
Docs say you should conform on the class
line after the :
but that's usually where the superclass goes. Another :
doesn't work. Using a comma separated list after the superclass also doesn't work
Also must adopt all required methods of each protocol, which I wasn't initially doing.
It is just described as a methods or properties skeleton instead of implementation. Methods and properties implementation can further be done by defining classes, functions and enumerations. Conformance of a protocol is defined as the methods or properties satisfying the requirements of the protocol.
A protocol defines a blueprint of methods, properties, and other requirements. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. But there would be a time when you want to restrict protocols to be adopted by a specific class.
Protocol InheritanceA Swift protocol can inherit from other protocols, requiring conforming types to provide implementations for all the property and method requirements in the entire protocol hierarchy.
In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object.
You use a comma:
class GameList: UIViewController, UITableViewDelegate, UITableViewDataSource { // ... }
But realize that the super class must be the first item in the comma separated list.
If you do not adopt all of the required methods of the protocol there will be a compiler error. You must get all of the required methods!
As XCode6-Beta7 releases,
I noticed the protocol method of UITableViewDataSource changed a little bit and sounded the same conform to protocol error which worked fine in beta6.
These are the required methods to be implemented according to the UITableViewDataSource protocol:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // insert code} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // insert code }
You might want to re-check the difference or re-implement the delegate method that you thought you just implement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With