Welcome to part 2 of the Swift Extensions posts. If you haven’t already, check out the previous post Swift Extensions Part 1 — Computed Properties and Initializers, to learn about those aspects and get an introduction to what Swift extensions are, and why you might want to use them. The short reason is they let you extend a type for which you may not have the source code for, and you may want to do so to make working with that type easier in your own code.
Are the initializers in the main type long with a bunch of options that are unnecessary for your code? Add your own convenience initializer that only takes the parameters you need to change, and assigns defaults to the parameters you don’t care about in the real type’s initializers. I was making several different UIColor objects in one of my apps, and they never needed alpha changed, so I just hard-coded that to be 1.0 in an Objective-C category (the precursor to Swift extensions). Of course, this is only really helpful if you call the longer code several times, which was the case for my app.
[Read more…]