Extending UIColor

After writing my previous article: “Subscripting Shortcuts” I tried to come up with a good usage of string subscripts in iOS, and decided a good example was to boost the power of UIColor by adding a couple of initializers that catered for two more widespread colour notations that are more common in the design and web industries: 255-RGB and HEX.

In this tutorial we will make use of our favourite tool, categories, to extend the base power of the framework’s UIColor class and allow for new ways of creating colours. Bear in mind that these initializers are geared towards speeding up development processes in sacrifice of  performance, specially when it comes to communications between developers and the design team. They will inherently produce a very small performance overhead on any app that adopts them (specially the hex conversion, as we will be doing string parsing and string parsing is ALWAYS expensive). Having said that, unless we iterate though a huge amount of colour objects, we can safely consider the knock-on effect as dismissible.

Continue reading “Extending UIColor”

Subscripting String Shortcuts

The use of subscripts in Swift has added a few nice features to its predecessor in Obj-C, not only for reading, but also for writing your own implementations.

If you are not familiar with subscripts, they define the logic that allows you to write shortcut accessors and setters for objects that can be understood as a collection, and are most commonly used in Arrays and Dictionaries:


var array = ["Hello", " ", "World", "!"]
array[2] //subscript returns "World"
array[2] = "Everybody" //The word "World" is now replaced with "Everybody" using a subscript

Continue reading “Subscripting String Shortcuts”

Smarter Numbers. Annex 1: Custom operators.

On my earlier post, “Smarter Numbers”, we talked about using operator overrides in swift to extend the functionality of framework classes.

There’s a bit more to it than what was discussed in the article, but we’ve decided to keep it as a separate post due it’s less “orthodox” usage: Swift also allows you to, in what could be considered as a controversial feature, create custom operators. This means that not only can you change the behaviour of the language. you can also change the language itself*.

Continue reading “Smarter Numbers. Annex 1: Custom operators.”

Smarter Numbers

When it comes to iOS app Development, I find one of the most commonly used framework class is NSNumber.

Which is ironic, because it’s a pretty dumb class. Really… there’s not much you can do with it except store numbers and check for equality. Albeit it being all over the place, it’s a pretty useless wrapper object. Whenever we need to edit the wrapped value we need to go through overly complicated statements like this:


number1 = NSNumber(float: number1.floatValue + number2.floatValue)

Fortunately for us, Swift is AWESOME.

Continue reading “Smarter Numbers”