How to make a class conform to a protocol in Swift?

Objective CSwift

Objective C Problem Overview


in Objective-C:

@interface CustomDataSource : NSObject <UITableViewDataSource>

@end

in Swift:

class CustomDataSource : UITableViewDataSource {
     
}

However, an error message will appear:

  1. Type 'CellDatasDataSource' does not conform to protocol 'NSObjectProtocol'
  2. Type 'CellDatasDataSource' does not conform to protocol 'UITableViewDataSource'

What should be the correct way ?

Objective C Solutions


Solution 1 - Objective C

> Type 'CellDatasDataSource' does not conform to protocol 'NSObjectProtocol'

You have to make your class inherit from NSObject to conform to the NSObjectProtocol. Vanilla Swift classes do not. But many parts of UIKit expect NSObjects.

class CustomDataSource : NSObject, UITableViewDataSource {

}

But this:

> Type 'CellDatasDataSource' does not conform to protocol 'UITableViewDataSource'

Is expected. You will get the error until your class implements all required methods of the protocol.

So get coding :)

Solution 2 - Objective C

A class has to inherit from a parent class before conform to protocol. There are mainly two ways of doing it.

One way is to have your class inherit from NSObject and conform to the UITableViewDataSource together. Now if you want to modify the functions in the protocol, you need to add keyword override before the function call, like this

class CustomDataSource : NSObject, UITableViewDataSource {

	override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
		
		// Configure the cell...
		
		return cell
	}
}

However this sometimes gets your code messy because you may have many protocols to conform to and each protocol may have several delegate functions. In this situation, you can separate the protocol conforming code out from the main class by using extension, and you do not need to add override keyword in the extension. So the equivalent of the code above will be

class CustomDataSource : NSObject{
	// Configure the object...
}

extension CustomDataSource: UITableViewDataSource {

	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
		
		// Configure the cell...
		
		return cell
	}
}

Solution 3 - Objective C

Xcode 9, helps to implement all mandatory methods of Swift Datasource & Delegates.

Here is example of UITableViewDataSource:

Shows warning/hint to implement mandatory methods:

enter image description here

Click on 'Fix' button, it will add all mandatory methods in code:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYuXuan FuView Question on Stackoverflow
Solution 1 - Objective CAlex WayneView Answer on Stackoverflow
Solution 2 - Objective CFangmingView Answer on Stackoverflow
Solution 3 - Objective CKrunalView Answer on Stackoverflow