Add a color, file, or image literal

Create literals in your playground code when the value of a color, file, or image does not need to change. You can use the same literal in multiple places in your playground. The types of literals are:

Literal

Description

Color

A UIColor or NSColor color value.

File

An NSURL reference to a file.

Image

A UIImage or NSImage reference to an image file.

Add a color literal

  1. Open the source editor for the playground page that will contain the literal.

  2. Place the insertion point at the location in the code for the UIColor or NSColor.

  3. Choose Editor > Insert Color Literal.

    A color literal is added at the insertion point, and the color picker opens.

  4. Choose the desired color from the color picker.

Double-click the color literal to change the value.

Add a file literal

  1. Open the source editor for the playground page that will contain the literal.

  2. Select the desired file in the Finder or in the playground, and drag it to the location in the source editor where you want to add the file literal.

    The insertion point tracks the pointer by moving to the nearest location in the code where the file literal can be placed.

Adding a file literal to a playground adds the referenced file to the top-level resources folder.

In the following screenshot, myFile is set to a file literal that references the file A llama poses.txt. The file is dragged into the playground from the Finder. The file is copied to the top-level resources folder of the playground.

Add an image literal

  1. Open the source editor for the playground page that will contain the literal.

  2. Select the desired file in the Finder or in the playground, and drag it to the location in the source editor where you want to add the file literal.

    The insertion point tracks the pointer by moving to the nearest location in the code where the image literal can be placed.

Adding an image literal to a playground adds the referenced file to the top-level resources folder.

In the following screenshot, myImage is set to an image literal that references the file A llama poses.png. The file is dragged into the playground from the Finder. The file is copied to the top-level resources folder of the playground.

Copying and moving literals