Swift Language Reference Flashcards
Learn all the Language Reference Topics of SwiftLang.
What is Lexical Structure?
Describes what sequence of characters form valid tokens of the language.
What is a token?
Consists of an identifier, keyword, punctuation, literal, or operator.
What is a valid token?
Form the lowest-level building blocks of the language and are used to describe the rest of the language
How are tokens are generated?
Generated from the characters of a Swift source file by considering the longest possible substring from the input text, within the constraints of the grammar. This behavior is referred to as longest match or maximal munch.
What is an Identifier?
Uppercase or lowercase letter A through Z, an underscore (_), a noncombining alphanumeric Unicode character in the Basic Multilingual Plane, or a character outside the Basic Multilingual Plane that isn’t in a Private Use Area. After the first character, digits and combining Unicode characters are also allowed.
How to use a reserved word as an identifier?
put a backtick (`) before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks aren’t considered part of the identifier; `x` and x have the same meaning.
how to refer to parameters inside a closure with no explicit parameter names?
$0, $1, $2, and so on.
Keywords used in declarations:
associatedtype, class, deinit, enum, extension, fileprivate, func, import, init, inout, internal, let, open, operator, private, protocol, public, static, struct, subscript, typealias, and var.
Keywords used in statements:
break, case, continue, default, defer, do, else, fallthrough, for, guard, if, in, repeat, return, switch, where, and while.
Keywords used in expressions and types:
as, Any, catch, false, is, nil, rethrows, super, self, Self, throw, throws, true, and try.
Keywords used in patterns:
_.
Keywords that begin with a number sign (#):
available, #colorLiteral, #column, #else, #elseif, #endif, #error, #file, #fileLiteral, #function, #if, #imageLiteral, #line, #selector, #sourceLocation, and #warning.
Keywords reserved in particular contexts:
associativity, convenience, dynamic, didSet, final, get, infix, indirect, lazy, left, mutating, none, nonmutating, optional, override, postfix, precedence, prefix, Protocol, required, right, set, Type, unowned, weak, and willSet. Outside the context in which they appear in the grammar, they can be used as identifiers.
The following tokens are reserved as punctuation and can’t be used as custom operators:
(, ), {, }, [, ], ., ,, :, ;, =, @, #, & (as a prefix operator), ->, `, ?, and ! (as a postfix operator).
What is a Literal?
A literal is the source code representation of a value of a type, such as a number or string.
The following are examples of literals:
42 // Integer literal
3.14159 // Floating-point literal
“Hello, world!” // String literal
true // Boolean literal