Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a class conform to a protocol in swift?

Tags:

I need to make a class conform to a protocol in Swift in order to implement a delegate. How would I do so?

like image 991
Phillip Avatar asked Jun 03 '14 20:06

Phillip


People also ask

Can a protocol inherit from a class Swift?

Protocols allow you to group similar methods, functions, and properties. Swift lets you specify these interface guarantees on class , struct , and enum types. Only class types can use base classes and inheritance from a protocol.

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.

Can Objective C class conform to Swift protocol?

Mitrenegades solution is to use an objective-c protocol, is one way, but if you want a swift protocol, then the other would be to refactor the code so as to not use the objective-c class directly, but instead use the protocol (e.g. some protocol based factory pattern). Either way may be appropriate for your purposes.

What's the difference between a protocol and a class in Swift?

You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.


2 Answers

class YourClass: SuperClassIfAny, FirstProtocol, SecondProtocol {
}

Note, though, that some protocols require you to implement delegate methods. For instance, UITableViewDataSource requires you to implement

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int

and

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!

If those are not implemented by the class conforming to the protocol, Xcode will give you a compile error (always check the protocol declaration, Cmd + Click will show you what methods you must implement).

like image 172
Oscar Swanros Avatar answered Nov 02 '22 22:11

Oscar Swanros


Xcode 6 beta 7 changed the protocol slightly for UITableViewDataSource to match the following syntax on the two required implementations:

6b7 : UITableViewDataSource

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!

compared to 6b6

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell

Of key difference is that UITableView, NSIndexPath and UITableViewCell are no longer 'Implicitly Unwrapped Optionals'

like image 39
Tarei SuperChur King Avatar answered Nov 02 '22 21:11

Tarei SuperChur King