Basics Flashcards

1
Q

Swift combines the best of:

A

C and Objective-C.

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

Printing text

A

print(“Hello, world!”)

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

Print the value of a variable inside a text

A

place it in parentheses, and insert a backslash just prior to the opening parenthesis:

print(“The value is (myVariable)”)

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

For printing only the variable value:

A

print(myVariable)

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

The keyword to declare a variable

A

The keyword var

The following example declares a variable, “a”, and assigns its value as “42”.

var a = 42

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

It is possible to change variable values over time:

A
a = 88
// Now "a" has a value of 88.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Declare a constant

A

using the let keyword.
This example declares a constant named “one” and assigns it a value of 1:

let one = 1

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

Declare multiple constants on a single line

A

separate them with commas:

let x = 0.0, y = 0.0, z = 0.0

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

Can constant and variable names contain almost any character?

A

Constant and variable names can contain almost any character, including Unicode characters.
However, constant and variable names must have no blank spaces, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line and box-drawing characters. Numbers can appear anywhere within a name, except for at the beginning.
Constants or variables of a certain type can’t be declared again with the same name, nor can they be altered to store values of differing types. Also, a constant cannot be made a variable, nor can a variable be made a constant.

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

Type annotations?

A

Type annotations ensure that your code is clear about the value stored within your constant or variable. Swift’s basic types include:

  • *Int**: Integers
  • *Double and Float**: Floating-Point Values
  • *Bool**: Boolean Values
  • *String**: Textual Data.

Add a type annotation by placing a colon (:) after the constant or variable name, then add a space, and then add the type name, as follows:var welcomeMsg: String
welcomeMsg = “Hello”

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

Are type annotations necessary?

A

In practice, you will rarely need to add type annotations. Providing an initial value for a constantor a variable at the point at which it is defined, will almost always be sufficient for Swift to infer which type should be used:

let pi = 3.14159 //Double

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

Basic Operators?

A

An operator is a special symbol or phrase used to check, change, or combine values.

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

Unary Operator

A

Has a single target (-a). A unary prefix operator is placed before the target (!b). A unary postfix operator is placed after the target (i++).

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

Binary Operator

A

Has two targets (4 + 5) and is infixed, appearing between the two targets.

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

Ternary Operator

A

Has three targets. Like C, Swift has one ternary operator, the ternary conditional operator (a ? b : c).

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

Assignment Operator

A

assignment operator (a = b) initializes or updates the value of a with the value of b:

let b = 7<br></br>var a = 42<br></br>a = b // a is now equal to 7

17
Q

The addition operator is also supported for

A

String concatenation:
“Hello, “ + “world” // equals “Hello, world”

18
Q

Remainder Operator

A

The remainder operator (a % b) calculates the number of multiples of b that fit within a, and returns the value that is left over, or the remainder.

9 % 4 // equals 1

Unlike the remainder operator in C and Objective-C, Swift’s remainder operator also operates on floating-point numbers:

8 % 2.5 // equals 0.5

19
Q

Increment and Decrement Operators

A

Swift’s increment operator (++) and decrement operator () are used as shortcuts that increase or decrease the value of a numeric variable by 1.

var i = 0<br></br>++i // i now equals 1

If the operator appears before the variable, it increments the variable before returning its value.
If the operator appears after the variable, it increments the variable after returning its value.

Like C, Swift provides compound assignment operators that combine assignment (=) with another operation.

var a = 1<br></br>a += 2<br></br>// a is now equal to 3

20
Q

Swift supports all of the standard comparison operators in C:

A

Equal to (a == b)
Not equal to (a != b)
Greater than (a > b)
Less than (a < b)
Greater than or equal to (a >= b)
Less than or equal to (a <= b)

Each of the comparison operators returns a Bool value indicating whether or not the statement is true

21
Q

Ternary Conditional Operator

A

The ternary conditional operator is a special operator with three parts, taking the form (question ? answer1 : answer2).

This operator acts as a shortcut in evaluating one of two expressions, based on whether the question is true or false. For a true question, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value.gender == 0 ? print(“male”) : print(“female”)
The example above will evaluate the expression gender == 0. If true, it prints “male”. Otherwise, it prints “female”.

22
Q

Range Operators

A

Swift offers two range operators, which are shortcuts for expressing a range of values.

The closed range operator (a…b) defines a range running from a to b, and includes the values a and b. The value of a must not be greater than that of b.

1…3 //1, 2, 3

The half-open range operator (a..defines a range that runs from a to b, but does not include b. As with the closed range operator, the value of a must not be greater than that of b.

1..<3 //1, 2

23
Q

Logical Operators

A

Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages:

Logical NOT operator (!a): Inverts a Boolean value so that true becomes false and false becomes true.

Logical AND operator (a && b): Creates logical expressions in which both values must be true for the overall expression to be true.

Logical OR operator (a || b): An infixed operator made from two adjacent pipe characters. It creates logical expressions in which only one of the two values has to be true for the overall expression to be true.

24
Q

Optionals

A

Optionals are used in situations in which a value may be absent.
An optional says:
-There is a value, and it equals x
or
-There isn’t a value at all

var myCode: Int? = 404

An optional Int is written as Int?, not Int. The question mark indicates that the value contained within is optional, meaning that it might contain some Int value, or it might contain no value at all.

25
Q

You set an optional variable to a valueless state by assigning it the special value…

A

You set an optional variable to a valueless state by assigning it the special value nil:

<span>var myCode: Int? = 404<br></br>myCode = nil<br></br>// myCode now contains no value</span>

An optional variable with no default value is automatically set to nil for you:

var someMsg: String?<br></br>// someMsg is automatically set to nil

nil cannot be used with non-optional constants and variables. If your code contains a constant or variable that needs to work with the absence of a value under certain conditions, always declare it as an optional value of the appropriate type.