Alan Alan - 7 months ago 12
Swift Question

Swift Extensions

I am curious as to what is the purpose of writing extensions for a class that you can already add code?

For example, in Ray Wenderlich's AlamoFire tutorial,

He has a ViewController class, but he writes an extension for that ViewController for his Networking Functions and

methods. Is the only reason he is writing extensions is for the purpose of dividing up code into logical bits?

Is this a common practice or just personal preference?


This is something Apple does too. If you look at the code (let's say the Array struct) you can see that every conformance to a protocol is encapsulated inside an extension.

The only difference is that your code is better separated. But it really is up to you.