Swift UIImage With Enum

Joseph Collins
iOS Development in Swift
3 min readSep 12, 2015

--

A few days ago I watched a talk given by Ayaka Nonaka on Swift Scripting. If you haven’t seen it yet, I highly recommend it! She describes how Venmo took advantage of Swift scripting to optimize their workflow with designers. Using a category, or an extension in Swift, for UIImage should be a required practice. Now let’s get down to business.

Session 411: Swift in Practice explains using strong types for UIImage as well as segue id’s. Check it out. Or follow along here for the image bit…

So first I needed some images…and I needed to figure out how to add them to a playground. I‘m a shameless minionnapper, I’ll admit it.

Disclaimer: I have minionnapped these from @natashatherobot. Mwuahahahaaaaa!

Next define an extension on UIImage:

And create a non-failable convenience initializer in the UIImage extension:

How to use:

The benefits:

Centrally located constants (that can be automated using swift scripting)

Doesn’t pollute global namespace

Have to use one of the enum constants

Image initializers are not failable

- Session 411

What does that mean?

Less strings, less bugs, less work for you. Start taking advantage of that Swift compiler!

But wait, there’s more!

I’m still learning all the different things you can do with the playground and today I created a shortcut to quickly show the results inline! (See above photo)

Go to the key bindings tab in Xcode preferences and search for “inline”. Give it a hotkey…

Then place your cursor on the line you want to show and

One More Thing

Sometimes you have to change your result display mode to see everything. Easy enough, just right click on the result and select “Value History”.

Happy Coding

Follow me @iJoeCollins on Twitter

--

--