4

As I was typing Color in the class name placeholder of a class declaration autocomplete-block, a color picker popped up. It allowed me to place a little colored square directly in the source code.

I've tried to google it but couldn't find any adequate results, so I'm asking what this feature is used for and how you would use these squares within source code. Can they be used as identifies, e.g. as class names? Can you set the value of e.g. SKColors using these squares? What's the deal?

enter image description here

Using Xcode 8.2.1

Double M
  • 1,449
  • 1
  • 12
  • 29

3 Answers3

3

This is the "Color literal" feature. It's similar to Image literals, described elsewhere on Stack Overflow.

You basically get a color picker and select a color which will be previewed in a tiny square.

Reference from Apple:

Literals are the actual values of your data represented in their native format, directly within the Xcode editor. For instance, there’s no need to type “myImage.jpg” in the editor – just drag your image from the Finder and the actual image will appear in-line with your code. Instead of showing RGB values to indicate color, the playground will render a color swatch. Literals in playgrounds behave similarly to code you would otherwise hand-author in regular Swift code, but are rendered in a much more useful manner.

jscs
  • 63,694
  • 13
  • 151
  • 195
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
2

After digging some more, I found out that these are Color Literals.

Double M
  • 1,449
  • 1
  • 12
  • 29
1

As mentioned by others, those are called color literals. You can use them anywhere you would use a UIColor (for iOS, tvOS or watchOS) or NSColor (for macOS) value.

They are not valid identifiers for class or variable names.

You can use them to directly set an SKColor value, since SKColor is just a typealias for UIColor or NSColor.

Matusalem Marques
  • 2,399
  • 2
  • 18
  • 28