0

A lot of places I see extensions like this

class MyClass: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension MyClass: SomeProtocol {
    func someFunc { }
}

Is there a good reason why the class is extended, instead of just conforming to the protocol the class itself?

class MyClass: UIViewController, SomeProtocol {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func someFunc {}
}
Alexander
  • 59,041
  • 12
  • 98
  • 151
perage
  • 118
  • 1
  • 12

2 Answers2

1

Technically (i.e., in effect), there is no difference. The two methods will behave the same. However, this allows the programmer to put all the code required for the protocol in one place.

tl;dr: it's a stylistic choice.

Sam
  • 2,350
  • 1
  • 11
  • 22
0

It groups members of the same protocol conformance together, replacing the need for labeling sections with // MARK:.

Alexander
  • 59,041
  • 12
  • 98
  • 151