Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to implement Swift protocols?

I have two options when implementing protocol conformance in Swift, with the same end result:

  • Implement the protocol within the class - that is, state the conformance at the top of class definition, and put implementation inside the class body, or
  • Implement the protocol in an extension - that is, code up protocol conformance entirely outside the class.

Here is an example:

public class MyClass : CustomDebugStringConvertible {
    ... // Something
    public var debugDescription : String {
        return "MyClass"
    }
}

vs.

class MyClass {
    ... // Something
}
extension MyClass : CustomDebugStringConvertible {
    public var debugDescription: String {
        return "MyClass"
    }
}

Code samples in Swift books tend to concentrate on the first approach; Apple's source code of Swift core reveals that they use only the second approach (see Bool and Optional for an example).

Is there a sound way to decide between the two approaches depending on the situation, or is it simply a matter of coding preference?

like image 920
Sergey Kalinichenko Avatar asked May 17 '16 16:05

Sergey Kalinichenko


People also ask

How do I add a protocol in Swift?

To create a protocol, use the protocol keyword followed by the name you want and defined by the curly braces. Protocols can be of 2 types: read-only/read-write. Read-only means you can only get the variable, but you cannot set it. Read-write means you can both set and get properties.

CAN protocols have implementation Swift?

As mentioned in Swift's documentation: “You can use protocol extensions to provide a default implementation to any method or computed property requirement of that protocol.” In fact, not only can you provide a default implementation to methods or computed properties defined in the protocol, you can also add new ones.

Why do we use protocols in Swift?

Protocols are used to define a “blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.” Swift checks for protocol conformity issues at compile-time, allowing developers to discover some fatal bugs in the code even before running the program.

What is protocol method in Swift?

In Swift, a protocol defines a blueprint of methods or properties that can then be adopted by classes (or any other types). We use the protocol keyword to define a protocol. For example, protocol Greet { // blueprint of a property var name: String { get } // blueprint of a method func message() }


2 Answers

It's more a matter of coding preference and readability. If you think your class is going to be giant, it might make more sense to implement it in an extension so that it's methods do not add clutter to your class. If it is a short class, I would say all in one, because readability is less affected.

like image 171
Ryan Collins Avatar answered Sep 28 '22 18:09

Ryan Collins


I see it mostly as coding preference. In my team here we have started to adopt the second approach. At first I thought it was an odd use of extension but I have come to like it. It keeps the implemented methods of a protocol nicely together and gives the impression that the class itself is smaller (just optics really). I could see some complications or opportunities for confusion if, say, the class has a tableview and you use extensions to implement the datasource and delegate. If someone then subclasses that class, they might not be aware of the extension and see unexpected behavior.

like image 20
onnoweb Avatar answered Sep 28 '22 20:09

onnoweb