0

The Alamofire API has extensions such as extension Request in ResponseSerialization.swift. When designing a Swift API why would you take this approach as opposed to just adding these methods to the Request class (Request.swift)?

I understand the use of extensions to extend API's when you don't control the source. This question is about using them to extend your own API.

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
  • All the extensions in this file are needed in the context of ResponseSerializer, therefore their "natural place" fit's better in ResponseSerializer.swift then in Request.swift. – Darko Apr 03 '16 at 10:28

2 Answers2

0

For cleanliness or adding functionality to other classes you didn't create (i.e. extension UIColor). You can create separate extensions to add in separate bits of functionality.

For example, if you have a UIViewController and you add a table view to it's view, instead of making the declaration of ViewController look like this:

class ViewController : UIViewController, UITableViewDataSource, UITableViewDelegate

you could separate it all with extensions so you don't clutter your ViewController file.

Like so:

extension ViewController : UITableViewDataSource, UITableViewDelegate

which you could separate from the main body of the ViewController class or extract into a new file.

Wes
  • 1,032
  • 7
  • 11
  • 1
    I am afraid you missed the point as the OP is probably asking why would somebody extend their own classes, not somebody else's classes. – Earl Grey Apr 02 '16 at 21:30
  • @EarlGrey yes, I referenced extending your own class with my View Controller example, a reason why one may want to extend their own classes is for cleanliness and organization in their code. – Wes Apr 02 '16 at 21:35
0

Extensions are one of the best features of Swift programming language and there are several use cases for them. Once you hold a grip of them you can come up with some really good and understandable code. Some of the use cases are:

1. Extending system types

With Swift you can any system type like Int or String to make some code more readable and to get some more functionality you would otherwise have to write on your own. For example, check out following code that repeats some task a number of times:

extension Int {
    func repetitions(task: () -> Void) {
        for _ in 0..<self {
            task()
        }
    }
}

Instead of creating separate function for task repetition and managing several parameters you can just extend Int and make it more readable:

3.repetitions({
    print("Hello!")
})

// Hello!
// Hello!
// Hello!

Everybody can agree that this is the simplest and cleanest code you can ever create.

2. Make messy code readable

Check the following definition:

class MyTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {
   // a lot of functions 
}

If you put everything inside one class or structure, code will get messy at one point and it'll be hard to track which method belongs to which protocol or class. Instead you should be using this:

class MyTableViewController: UIViewController {
}

extension MyTableViewController: UITableViewDelegate {
}

extension MyTableViewController: UITableViewDataSource {
}

// etc...

3. Protocol extensions

Protocol extension are one of the coolest features of Swift. They enable adding methods to any class that adopts the protocol you are extending. For example let's extend CollectionType protocol.

extension CollectionType {
   func printAll() {
      print(self)
   }
}

Now you can use method printAll() on any structure that adopt this protocol! Some of them are native Swift types like Array, Dictionary or Set.

These are just some of the main usages of extensions and they can do a lot more:

  • Add computed instance properties and computed type properties
  • Define instance methods and type methods
  • Provide new initializers
  • Define subscripts
  • Define and use new nested types
  • Make an existing type conform to a protocol
Said Sikira
  • 4,482
  • 29
  • 42