Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conform to protocol in ViewController, in Swift

Tags:

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

EDIT:

Also must adopt all required methods of each protocol, which I wasn't initially doing.

like image 642
Daddy Avatar asked Jun 07 '14 00:06

Daddy


People also ask

What is protocol conformance in Swift?

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.

Is it possible to prevent the adoption of a protocol by a struct?

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.

CAN protocol inherit in Swift?

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.

What is a delegate in Swift programming?

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.


2 Answers

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!

like image 98
Firo Avatar answered Sep 27 '22 19:09

Firo


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.

like image 42
Berby Huang Avatar answered Sep 27 '22 20:09

Berby Huang