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

Gurdeep Singh
4 min readDec 7, 2016

--

Image taken from https://developer.apple.com/swift/ (Edited)

#imageLiterals, #colorLiterals

Thanks to Erica Sadun, we have a rejuvenated way of working with UIColors & UIImages (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 UIColors 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 UIColors 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…

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.

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.

Cool!!! No more UIImage(named: "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 UIImage(named: String) 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…

For ones, who are interested, below are the new initializers for UIImage and UIColor , 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 #fileLiteral , 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 #colorLiteral and #imageLiteral 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, #colorLiteral or #imageLiteral. 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

--

--