Where can I find the usage of `#fileLiteral`?

also #colorLiteral and #sourceLocation

These are special syntax used by playgrounds. They're not meant to be written by hand, although it is possible write them. This article from Xcode Help shows how they're used in a playground: Add a color, file, or image literal

There's a brief reference for them in the formal grammar in the Swift documentation.

playground-literal → #colorLiteral ( red : expression , green : expression , blue : expression , alpha : expression )
playground-literal → #fileLiteral ( resourceName : expression )
playground-literal → #imageLiteral ( resourceName : expression )

1 Like

How did you get this knowledge? and sourceLocation?

They of cource also work in an Xcode project. Let code completion help you out by starting to type the name of a literal i.e. colorLit.... For instance, for a color literal, hitting enter on the completion will form a color swatch. You can pick a color from the panel by double-clicking on the swatch. Note that it requires importing UIKit.

This is how it looks like:

Some docs on #sourceLocation can be found in this section: Compiler Control Statements

Image Literal can not form a Swatch, and file Literal hasn't a code completion hint. xcode version 10.0 beta (10L176w)

Please be a bit more specific about what you're doing.
fileLiteral and sourceLocation indeed don't have a completion, write them explicitly according to the grammar defined in the docs.swift.org docs @Alex_Martini and I linked. #fileLiteral will show an appropriate file icon once correctly spelled, i.e. #fileLiteral(resourceName: "validPath"), and will represent a URL. The color literal represents a UIColor.

Here is how the completion looks like for a color literal.

I'm sorry for "doesn't work",I would try my best to describe what I am doing.
I'm learning the swift by reading the swift language guide, so I want to try it as the guide write.that's all.

Alright, let's at least succeed with the color literal. Are you still confused about how to use it? Does the completion appear when you start typing colorLiteral, the way it does in the image I attached?

@Alex_Martini linked a great document (this one) that guides you through the interactive way of using these literals. Apart from that, all you need to know is that they can also be written explicitly according to the grammar defined in the documentation. A guide is a quick walk through the docs, so the presence of a detailed discussion on these literals in the documentation does not imply its presence in the guide.

I got this information because I'm part of the team that maintains the Swift documentation :slight_smile:

For information about #sourceLocation please see Line Control Statement.