TTS Swift Notes: The Basics Flashcards

1
Q

What does Int stands for?

A

Integer

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

A Double accounts for what?

A

A floating-point value up to 15 spaces after the decimal.

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

A Float accounts for what?

A

Numbers with decimals at least 6 spaces after the decimal.

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

What does Bool stand for?

A

Boolean

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

What does a String represent?

A

Textual data.

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

What do tuples do?

A

They enable the creation and passing around of groupings of values. You can use a tuple to return multiple values from a function as a single compound value.

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

Optionals return what two values?

A

There is a value and it equals x OR there isn’t a value at all.

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

What types do optionals work for?

A

All, not just classes.

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

Can the value of a constant be changed once it’s been set?

A

No.

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

What keyword declares a constant?

A

let

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

What keyword declares a variable?

A

var

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

What the is proper way to name a variable/constant? currentLoginAttempt or current_login_attempt?

A

The first.

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

Can you declare multiple constants or variables on a single line?

A

Yes

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

How do you declare multiple constants or variables on a single line?

A

By separating them with commas.

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

What does type annotation do?

A

It is clear about the kind of values a constant or variable can store.

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

How do you write a type annotation?

A

By placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use. EX: var welcomeMessage: String

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

How do you read the following?: var welcomeMessage: String

A

“Declare a variable called welcomeMessage that is of type String.”

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

Can you define multiple related variables of the same type on a single line?

A

Yes

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

How do you define multiple related variables of the same type on a single line?

A

Separate the variables by commas, follow the last variable with a colon, then declare the type annotation. EX: var red, green, blue: Double

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

If you do not declare a type, how does Swift know what type it is?

A

It infers it.

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

What five items can constant and variable names not contain?

A
  1. Whitespace characters
    1. Mathematical symbols
    2. Arrows
    3. Private-use (or invalid) Unicode code points
    4. line- and box-drawing characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What can constant and variable names not start with?

A

A number.

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

Once you’ve declared a constant or variable of a certain type, can you redeclare it again with the same name or change it to store values of a different type?

A

No.

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

Can you change a constant into a variable or vice versa?

A

No.

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

What command prints the current value of a constant or variable?

A

print()

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

Swift uses string interpolation to do what?

A

To include the name of a constant or variable as a placeholder in a longer string and to prompt Swift to replace it with the current value of that constant or variable.

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

How do you call string interpolation?

A

()

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

How do you comment out lines?

A

//

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

How do you comment out multiple lines?

A

/* */

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

How do you nest comments?

A

Start a multiline comment block and then start a second multiline comment within the first block.

/* this is the start of the first multiline comment
/* this is the second, nested multiline comment */
this is the end of the first multiline comment */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What are required if you want to write multiple separate statements on a single line?

A

semicolons

  • let cat = “🐱”; print(cat)
  • // prints “🐱”
32
Q

What are integers?

A

Whole numbers with no fractional component, such as 42 or -23.

33
Q

What is a signed integer?

A

One that is positive, zero, or negative.

34
Q

What is an unsigned integer?

A

One that is positive or zero.

35
Q

Swift provides signed and unsigned integers in what four bit forms?

A

8, 16, 32, and 64.

36
Q

What does UInt8 stand for?

A

8-bit unsigned integer.

37
Q

What does Int32 stand for?

A

A 32-bit signed integer.

38
Q

What is the minValue of UInt8.min?

A

0

39
Q

What is the max value of UInt8.max?

A

255

40
Q

What-bit floating-point number does a Double represent?

A

64-bit

41
Q

What-bit floating-point number does a Float represent?

A

32-bit

42
Q

How does Swift assign floating-point numbers by default?

A

As a Double.

43
Q

If you combine an integer and a floating-point literals in an expression, what type will Swift assign it?

A

As a Double.

44
Q

In what four ways can an integer literal be written?

A
  • A decimal number, with no prefix
  • A binary number, with a 0b prefix
  • An octal number, with a 0o prefix
  • A hexadecimal number, with a 0x prefix
45
Q

How do you write an exponent?

A

With an uppercase or lowercase e. EX: 1.25e2 means 1.25 X 10squared

46
Q

Both integers and floats can be padded with what two things to improve readability?

A

Extra zeros and underscores. EX: 000123.456, 1_000_000

47
Q

What are the four reasons to use specific integer types (i.e., not just Int)

A

If there is explicitly-sized data from an external source, for performance, for memory usage, or for other necessary optimization,

48
Q

An Int8 constant or variable can store numbers between what and what?

A

-128 and 127

49
Q

An UInt8 constant or variable can store numbers between what and what?

A

0 and 255

50
Q

How do you convert one specific number type to another?

A

Initialize a new number of the desired type with the existing value.

51
Q

What do type aliases do?

A

They define an alternative name for an existing type.

52
Q

How do you define type aliases?

A

With the type alias keyword.

53
Q

When are type aliases useful?

A

When you want to refer to an existing type by a name that is contextually more appropriate, such as when working with data of a specific size from an external source.

54
Q

What two Boolean constant values does Swift provide?

A

true and false

55
Q

What kind of values can a tuple have?

A

Any type and they do not have to be of the same type as each other.

56
Q

Can you name the individual elements in a tuple?

A

Yes

57
Q

When can you name the individual elements in a tuple?

A

When the tuple is defined.

58
Q

What is useful about naming tuple elements?

A

You can use the element names to access the values of those elements.

59
Q

What is particularly useful about tuples?

A

They are useful as the return values of functions.

60
Q

Are tuples for temporary or permanent groups of related values?

A

Temporary.

61
Q

If your data structure is likely to persist beyond a temporary scope, what are the best ways to model it?

A

As a class or as a structure.

62
Q

How is an option Int written?

A

Int?

63
Q

How do you set an optional variable to a valueless state?

A

Assign it the special value nil.

64
Q

If you define an optional variable without providing a default value, what is the variable automatically set to?

A

nil

65
Q

In Swift, what is nil?

A

Nil is the absence of a value of a certain type.

66
Q

What is forced unwrapping?

A

Accessing an underlying value by adding an exclamation point (!) to the end of an optional’s name. Effectively saying, “I know that this optional definitely has a value; please use it.”

67
Q

When should you use optional binding?

A

To find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.

68
Q

When is it useful to remove the need to check and unwrap the optional’s value every time it is accessed?

A

When it is clear from a program’s structure that an optional will always have a value, after that value is first set.

69
Q

What is an implicit unwrapped optional?

A

An optional that will always have a value.

70
Q

How do you write an implicitly unwrapped optional?

A

By placing an exclamation mark (String!) after the type that you want to make optional.

71
Q

When do you place an exclamation point after an optional’s type?

A

When you declare it.

72
Q

When is it not a good idea to use an implicit unwrapped optional?

A

If there is a possibility of a variable becoming nil at a later point.

73
Q

What is an assertion?

A

A runtime check that a Boolean condition definitely evaluates to true.

74
Q

When do you use an assertion?

A

To make sure that an essential condition is satisfied before executing any further code.

75
Q

What does an assertion let you provide?

A

A suitable debug message as to the nature of the assert.

76
Q

How do you write an assertion?

A

By calling the Swift standard library global assert(_:_file:line:)

  • let age = -3
  • assert(age >= 0, “A person’s age cannot be less than zero”)

// this causes the assertion to trigger, because age is not >= 0