2

If I have a class Christmas, and a protocol Merry, to make Christmas conform to Merry, many people would do it this way:

class Christmas {
    ...
}

extension Christmas: Merry {
    ...
} 

It's also encouraged by Apple.

However, isn't it more convenient to just make the class conform to protocol when it's defined? Like this:

class Christmas: Merry {
    ...
}

What's the difference between the 2 methods?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Bright
  • 5,699
  • 2
  • 50
  • 72
  • 1
    No big difference, first one is just neat as your root class won't need to handle all ado caused by protocol functions. – rptwsthi Dec 20 '16 at 07:17
  • 1
    I use it to keep my codes neat, placing the class core functions in the original file while all delegate methods in another. Also to extend existing classes as it is made for. – Ben Ong Dec 20 '16 at 07:23
  • Possible duplicate of http://stackoverflow.com/questions/36263892/extensions-in-my-own-custom-class or http://stackoverflow.com/questions/40502086/how-to-properly-use-class-extensions-in-swift. – Martin R Dec 20 '16 at 07:41
  • If you use extensions like that, beware this: https://stackoverflow.com/questions/55364212/overriding-non-objc-declarations-from-extensions-is-not-supported – algrid Nov 18 '20 at 13:44

3 Answers3

8

They are different coding styles. The first option

class Christmas {
   ...
}

extension Christmas: Merry {
    ...
}

is cleaner when you're looking at the whole class. You can instantly see all the protocol implementations the class conforms to.

Using the second option you put the implementation of the protocol inside the class mixing it with the class methods. However, if you use

//MARK: - 

the code becomes not less clean, than when you use extensions. For example:

protocol Merry: class {
    func celebrate()
}

class Cristmas: NSObject, Merry {
    private var santa: AnyObject?

    //MARK: - Private Methods
    private func callSanta() {
        //calling Santa
    }

    //MARK: - Merry Implementation
    func celebrate() {
        //celebration starts here
    }
}

and when looking at the whole class you clearly see the separation of the protocol implementation: enter image description here

The functionality of both options is the same.

Max Pevsner
  • 4,098
  • 2
  • 18
  • 32
  • 1
    I've been doing the `extension Foo: SomeProtocol` for years, but today I found out, the functionality of both options in **NOT**, in fact, the same. In the first example, if you subclass `Christmas`, you will not be able to override the `celebrate` function. Considering I was using the `extension` appraoch just for aesthetic purposes, I'm starting to question my decision. – Lord Zsolt Jan 18 '21 at 13:02
  • > is cleaner when you're looking at the whole class. You can instantly see all the protocol implementations the class conforms to. This will be super hard to track if your class is getting longer. Instead, putting the protocol conformance at the top level. – Honghao Z Aug 04 '23 at 22:58
1

There is difference between class conforms to protocol and the extension.

At the time of writing your class, you knew this should conforms to protocol then you can use class conforms to protocol. However, extensions are mean to extend functionality of existing classes. After writing your class and using it for a year, you need to add some additional feature to your class so instead of modify class, you can simply extend the class. Extension is not just for your classes, you can extend the behavior of every class available to you(native frameworks, third party libraries).

Anni S
  • 1,996
  • 19
  • 28
0

There is a little difference. The first one confirms to protocol as you expected. Coming to second one that also confirms to protocol and in addition to that methods writing in extensions

extension Christmas: Merry {
    ...
} 

like this will make those methods available through out project with that Class name "Christmas"

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
  • I don't get it, do you mean the first method doesn't " make those methods available through out project with that Class name "Christmas" " ? – Bright Dec 20 '16 at 07:25
  • yes. for an example creating extension for String class will make those methods available for any string class object in the whole project. – Sivajee Battina Dec 20 '16 at 07:28