How to Access Color and Image Literals in Swift

How to Access Color and Image Literals in Swift

There seems to be a bit of fake news circling the Swift community at the moment and I will clear it up here.

So, the word on the street is that Swift no longer supports Color and Image Literals.

This is NOT true.

As of this writing, you can still use both of these features, however they don’t show up on autocomplete anymore.


Here’s the syntax you need to make the magic happen!


Color and Image Literal Syntax


var colorLiteral = #colorLiteral(        

The opening parenthesis is intentional because as soon as you type it, it triggers the color literal symbol, which you double-click to get the colour picker.

Image via Author

The result is a UIColor that’s assigned to the variable colorLiteral.



//    var colorLiteral = #colorLiteral(red: 0.0431372549, green: 0.1843137255, blue: 0.6235294118, alpha: 1)        

Of course, all of this still translates to code, as you can see when you comment it out.

Note that this syntax also works for image literals.

Now that you know how to access color and image literals without autocomplete, you can also spread the word by linking to this post.


要查看或添加评论,请登录

Ijeoma Nelson的更多文章

社区洞察

其他会员也浏览了