So, there has been a reason for my most recent choice of topics to discuss. This isn’t some grand culmination post, but the topic for today is related to several of the previous topics due to what it can do. Back in Objective-C, you could extend classes for which you did not have the source code with something called categories. If you are curious about Objective-C categories, I wrote about them back when this blog was still about Objective-C in my post Objective-C Categories. This post was much earlier in my blogging career, so it may not be as polished as my current posts, so, you have been warned.
Anyway, Objective-C Categories gave you the ability to add additional methods to another class for which you didn’t have the original source code. I used this capability in a later Objective-C post Introduction to UIColor, to create a category that made it easier to create a UIColor for my purposes. I didn’t need to customize alpha (transparency) each time, and I wanted to use color values between 0 and 255 instead of 0.0 and 1.0, so I made a category that translated my 0 to 255 values to their 0.0 to 1.0 equivalents, and hard-coded the alpha to 1.0 (fully opaque).
These are not the same as iOS 8 extensions, such as the Today View Widgets, UIActivityViewController sharing extensions, etc. We will cover those in a later post.
When you look at the capabilities of Swift Extensions, they are basically doing the same thing as Objective-C categories, adding methods to existing classes. With one exception, they are all different flavors of methods.
[Read more…]