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

YuXuan Fu :

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 ?

Alex Wayne :

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 :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a swift class conform to a protocol that requires a protocol-conform type for one of its properties?

How to make an enum conform to a protocol in Swift?

Make swift class conform to protocol - at static/class level

How to conform a class to a protocol in Swift and can protocol methods be overridden?

How can I make my Objective-C class conform to Swift's `Equatable` protocol?

How to make Swift class conform to Objective-C protocol, without exposing it to Objective-C?

Make swift class conform to objective c protocol that defines properties

Make a swift protocol conform to Hashable

How do I make a protocol that my generic class can conform to?

How to make a generic class conform to a protocol for specific type?

How to make a class in Swift extend a concrete class and conform to multiple protocols?

Make property of type and also conform to protocol in Swift

Swift: Unable to make UIApplication conform to my protocol

Swift class does not conform to Protocol with Enum

How to make Xcode automatically conform to a protocol

How to make all subclasses necessarily conform to protocol?

How To Conform to the Sequence Protocol in Swift 4

How to conform to a protocol with a generic superscript affordance in Swift?

Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

How can I make Swift's String type conform to the CVarArgType protocol?

Swift Array of Class Types that Each Conform to a Protocol and are Subclasses of Common Class

How to create class methods that conform to a protocol shared between Swift and Objective-C?

Conform to protocol in ViewController, in Swift

Swift conform to protocol subclass

Swift does not conform to protocol

Class does not conform to protocol

Return Class conform to protocol

Swift - Core Data - codegen - Make the extension conform to a protocol?

Swift: Make two types with the same "shape" conform to a common protocol