Swift Language Reference Flashcards

Learn all the Language Reference Topics of SwiftLang.

1
Q

What is Lexical Structure?

A

Describes what sequence of characters form valid tokens of the language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a token?

A

Consists of an identifier, keyword, punctuation, literal, or operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a valid token?

A

Form the lowest-level building blocks of the language and are used to describe the rest of the language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How are tokens are generated?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an Identifier?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to use a reserved word as an identifier?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to refer to parameters inside a closure with no explicit parameter names?

A

$0, $1, $2, and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Keywords used in declarations:

A

associatedtype, class, deinit, enum, extension, fileprivate, func, import, init, inout, internal, let, open, operator, private, protocol, public, static, struct, subscript, typealias, and var.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Keywords used in statements:

A

break, case, continue, default, defer, do, else, fallthrough, for, guard, if, in, repeat, return, switch, where, and while.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Keywords used in expressions and types:

A

as, Any, catch, false, is, nil, rethrows, super, self, Self, throw, throws, true, and try.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Keywords used in patterns:

A

_.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Keywords that begin with a number sign (#):

A

available, #colorLiteral, #column, #else, #elseif, #endif, #error, #file, #fileLiteral, #function, #if, #imageLiteral, #line, #selector, #sourceLocation, and #warning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Keywords reserved in particular contexts:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The following tokens are reserved as punctuation and can’t be used as custom operators:

A

(, ), {, }, [, ], ., ,, :, ;, =, @, #, & (as a prefix operator), ->, `, ?, and ! (as a postfix operator).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Literal?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

To which protocols does an annotation for a literal value have to conform in order to be inferred?

A

Swift standard library protocols:
ExpressibleByIntegerLiteral for integer literals, ExpressibleByFloatLiteral for floating-point literals, ExpressibleByStringLiteral for string literals, ExpressibleByBooleanLiteral for Boolean literals, ExpressibleByUnicodeScalarLiteral for string literals that contain only a single Unicode scalar, and ExpressibleByExtendedGraphemeClusterLiteral for string literals that contain only a single extended grapheme cluster.

For example, Int8 conforms to the ExpressibleByIntegerLiteral protocol, and therefore it can be used in the type annotation for the integer literal 42 in the declaration let x: Int8 = 42.

17
Q

What are Integer Literals?

A

Integer literals represent integer values of unspecified precision.

18
Q

What are the integer literals types?

A
Decimal 0-9,
Binary begin with 0b from 0-1, 
Octal begin with 0o from 0-7, 
Hexadecimal begin with 0x from 0-9 A-F or a-f.
Negative -(0-9)
19
Q

What are underscores (_) used for in integers?

A

readability:
1_0 is equal to 10
001 is equal to 1
00_1 is equal to 1

20
Q

What are the Floating-Point Literals?

A

Floating-point literals represent floating-point values of unspecified precision.

21
Q

What is the default inferred type of a floating-point literal?

A

Unless otherwise specified, the default inferred type of a floating-point literal is the Swift standard library type Double

22
Q

How many bits is a Double floating-point number?

A

64-bit

23
Q

How many bits is a Float floating-point number?

A

32-bit

24
Q

How can String literals be declared?

A

single-line = “These are the same.”
multi-line = “””
These are the same.
“””

25
Q

What are the String scape sequences?

A
Basically using a backlash (\)
Null character (\0)
Backslash (\\)
Horizontal tab (\t)
Line feed (\n)
Carriage return (\r)
Double quotation mark (\")
Single quotation mark (\')
Unicode scalar (\u{n}), where n is a hexadecimal number that has one to eight digits