Swift Flashcards

1
Q

Declare implicitly as type Int: var year = 2015

A

2015

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

unknownString = 3

A

3 is NSObject

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

unknownString = “3”

A

“3”

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

If you know that the value of a variable won’t change again, you should use let. Here, myName is declared as a constant and initialized with the value Jesse: let myName = “Jesse”

A

“Jesse”

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

Declare as optional (i.e. empty): var height: Int? friend = nil

A

nil nil

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

To assign an optional an initial value: var friend: String? = “Joe”

A

“Joe”

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

To check if there’s a vale for or optional:if let someone = friend{ print(“Hello ” + someone)}

A

“Hello Joe”

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

var b = a * 3

A

15

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

var c = b/2

A

2

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

var c: Double = 5/2

A

2.5

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

c * 2

A

5

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

c * [integer 4]

A

10

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

c * Double(int)

A

10

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

Tuples are combined values that always appear together. Let person = (“Joe”, 24) print(person.0) print(person.1)

A

Joe 24

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

Often we want to refer to members in a tuple by name rather than index value (e.g. .0, .1, .2, etc.). let (name, age) = person print(name)

A

Joe

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

Tuple values may be named at the time of creation let person = (name: “Joe”, age: 24) print(person.name)

A

Joe

17
Q

Arrays are defined with brackets and commas: var arr = [1, 2, 3, 4, 5] arr = [0]

A

1

18
Q

arr.append(6) print(arr)

A

[1, 2, 3, 4, 5, 6]

19
Q

arr.removeAtIndex(2) print(arr)

A

[1, 2, 4, 5, 6]

20
Q

arr.removeLast() print(arr)

A

[1, 2, 4, 5]

21
Q

Ranges are indicated with ellipsis and their counts also begin at zero. arr.removeRange(1…2) print(arr)

A

[1, 5]

22
Q

var dict = [“name”: “Rob”, “age”: 34, “gender”: “male”] print(dict[“name”])

A

Optional(Rob)

23
Q

Use ! to force unwrap: print(dict[“name”]!)

A

Rob

24
Q

To add a value: dict[“hairColor”] = “brown”

A

{Some ‘brown’}

25
Q

var name = dict[“name”] var myString = “my name is ” + name

A

name is an NSObject (non string object)

26
Q

var myString = “my name is (name)”

A

“my name is Optional(Rob)”

27
Q

Force Unwrap var myString = “my name is (name!)”

A

“my name is Rob”

28
Q

for var i = 1; i

A

9 times

29
Q

var arr = [8, 3, 9, 91] for (index, value) in arr.enumerate() { arr[index] = value + 1 } print(arr)

A

[8, 3 , 9, 91] (4 times) [9, 4, 10, 92]/n

30
Q

var i = 1 while i

A

5 10 15 20 25 30 35 40 45 50