#color, #image Are The New Literals In The Cocoa Town

Gurdeep Singh
Dec 7, 2016 · 4 min read
Image for post
Image for post
Image taken from https://developer.apple.com/swift/ (Edited)

#imageLiterals, #colorLiterals

Thanks to Erica Sadun, we have a rejuvenated way of working with & (and also resource files !). With the release of Swift 3, one can simply use literals for colors and images also, instead of instantiating them with the regular initializers.

Wait… What’s a literal ? Let’s get some help from Wikipedia 😉 and check the exact definition !

literal • /ˈlɪt(ə)r(ə)l/ noun
In computer science, a literal is a notation for representing a fixed value in source code.

let num = 10           // 10 is an integer literal
let animal = "dog" // "dog" is a string literal
let pi = 3.1415 // 3.1415 is floating-point literal

With our basics brushed up, we can head back to the Cocoa town !


In the good old ObjC days, ColorSense used to be my all-time favorite. For most part of those days, this was the only Xcode-plugin on my mac. For ones, who haven’t used it, please watch the video below to see it’s magic:

The functionality seemed so essential to me, that I always felt that this should be an inbuilt feature in Xcode. Believe me, this was the thing I missed the most switching over to Swift as CodeSense didn’t work with Swift.

But finally, my prayers have been answered!!! Yayyyy… 🙃

Now we have a way of using with having a color preview WITH SWIFT TOO. It’s inbuilt into Xcode & Apple couldn’t have done it in a neater way.

Gone are those days, when we had to instantiate like this

let c = UIColor(red: 32.0/255.0, green: 210.0/255.0, blue: 101.0/255.0, alpha: 1.0)

Whole new world of literals awaits you…

Image for post
Image for post

Yes, the colored box you see, is the color literal. One can change the color as well, simply by double clicking and selecting a new value. If you just know the RGB values for your color, hit the button at the bottom saying “Other…”. It shall open up the color selection tool for Xcode where you can set any color you wish.

Image for post
Image for post
Color Picker

Apple just took it a step further and made our life so much simpler by providing the identical operation for Image assets also. If a picture is worth a thousand words, then a GIF would suffice a book. Have a look at the one below.

Image for post
Image for post

Cool!!! No more … (pun intended), No more lengthy and ugly UIColor initializers. Cherry on the cake : In-place preview for colors and images !


I’ve always hated Stringy-typed APIs, and is no exception! And… I’m not the only one here, developers around the world have been trying to find ways to write safer code with Image Assets. I’ve also been using this trick in a few of my projects, but gone are those days ! Apple has just…

Image for post
Image for post

For ones, who are interested, below are the new initializers for and , working behind the scenes.

extension UIColor {     @nonobjc required public convenience init(colorLiteralRed              red: Float, green: Float, blue: Float, alpha: Float)}// USAGE : #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)--------------------------------------------------------------------extension UIImage {     required public convenience init(imageLiteralResourceName name: String)}// USAGE : #imageLiteral(resourceName: "image-name")

I feel a bit bad for , as it is also among the new additions but hasn’t gained much of my attention. It provides the complete path to the file in the app’s bundle. For sake of completeness, let’s have a look at it also.

let file = #fileLiteral(resourceName: “filename.png”)

Personally speaking, Haven’t used it much but and are all over the place in my swift code nowadays.

Hope you’ll also find it useful.

TLDR;
Just open up Xcode and start typing, or . You’re in for a treat.😉

Go on and read the article if you like !

Thanks for reading, please hit the recommend icon if you like it 😉

Follow me on Twitter : https://twitter.com/Gurdeep060289

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store