Intro to Swift Flashcards

1
Q

What is swift?

A
  • An object oriented language which employs modern language theory concepts and exploits a number of object oriented principles.
  • It simplifies objective C
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the standard naming scheme for naming constants and variables in swift?

A
  • start with lowercase letter and use camel case
  • types can be inferred from default values or else you’ll need to specify them
  • no ; needed at the end of lines
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When do we use let and var when first defining/assigning values?

A
  • we use let for constants
  • we use var for variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does swift let you put in large integers to improve readability?

A
  • Lets you include _
  • for example = var speed = 186_000_000
  • If you print these, the _ doesn’t appear
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are standard types in swift?

A

String, Double, Int, Bool, non standard types (such a tuples)

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

How to generate numbers in swift?

A

Int.random(in: 1…6) // gives a value between 1 and 6

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

How do you assign values to a tuple?

A

let httpError: (code: Int, message: String) = (404, “Not Found”)
print (httpError.message)

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

How do we refer to components of a tuple numerically?

A

print (httpError.1)
print (httpError.0)

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

In C and objective C what are strings?

A

strings are arrays in ASCII characters

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

What are strings composed of in swift?

A

Strings are composed of unicode characters and each character may be represented by a multi-byte sequence

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

What type are strings?

A

Value types

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

What is an extended grapheme cluster?

A

An extended grapheme cluster is a sequence of one or more Unicode scalars that (when combined) produce a single human-readable character

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

How do you do multiline strings in swift?

A
  • use 3 quotation marks
  • ””” bla bla
    bla bbla
    bal “””
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What do you use to prevent different lines inside a multiline string?

A

a \

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

what can the # symbol be used for with strings in swift

A
  • for treating characters literally: #”Did you say “help me”?”# prints
    Did you say “help me”?
  • for inserting variables into a string: let user = “Phil”, let myString = #”Did you say “help me” (user) ?”#
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do we index a string?

A
  • using a string.Index or a range
  • can create an index toaccess a specific character based on those indexes?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can we access every single individual characters of a string

A

by iterating over the string itself

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

What function can we use to determine the length of a string?

A

.count

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

What does swifts use of extended grapheme clusters for character values mean?

A

That concatenating two strings may not increase the string’s character count

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

What type are optionals in swift?

A

Non-standard types

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

When are optionals used in swift?

A

They are used where a value may be absent e.g. trying to convert a string to an integer.

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

What happens when we define something as an optional variable?

A

It automatically initialises it’s value to nil

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

How is nil different to an empty string?

A

Empty value doesn’t mean “” (an empty string) it’s simply nothing, which we can’t return, that’s why we have to initialise it to NIL.

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

How do we specify that something has an optional type?

A

we use a ?
- so we could say:
var survey: String?

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

how do we check if a variable with optional type has a value?

A
  • Can use an “if” statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

If an variable with an optional types does have a value after checking if it does, how do we get that data?

A
  • By If it does we can unwrap it using a ! This is known as force unwrapping
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Why must we check if a variable has a value of NIL before force unwrapping it?

A
  • If we unwrap a NIL value, the program will crash.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is optional chaining?

A
  • put a ? instead of ! during the force unwrap
  • it will attempt to unwrap a NIL value but the program won’t crash
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How can ?? be used to carry out different outcomes?

A

If the thing on the left of the ?? evaluates to NIL, it returns the thing on the right of ??

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

What do you have to do when you create a variable?

A

When you create a variable it has to be initialised or made an optional variable so that it has a default value.

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

What are the collection types in swift?

A

Arrays, Sets, Dictionaries

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

What are the differences between sets, arrays and dictionaries

A
  • arrays are ordered collections of values
  • sets are unordered collections of unique values
  • dictionaries are unordered collections of key-value pairs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How do you define an array?

A
  • user square brackets
  • let a = [1,2,1,5,2,7,8,4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How do you define sets?

A
  • have to use set keyword
  • var b: Set<String> = ["blues","jazz","soul"]</String>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

How do you define a dictionary?

A
  • colons between key and value
  • var c = [2: “blue”, 3:”bluer”, 4:”bluest”]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is true about arrays and types?

A

Once an array has a type it cannot be changed

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

What does it mean that an array is dynamic?

A

The size and values of the array can change

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

How do we use different types in an array where each object must have the same type?

A
  • We declare the array as having type
  • as [any]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What number does indexing start at in arrays?

A
  • indexing starts at 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

How can we get the size of an array?

A
  • By using the .count function on it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

How can we return the 3rd element of an array?

A
  • by doing the arrayName[2]
  • because indexing starts at 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What keyword do we use to add an item to an array?

A

arrayName.append(newValue)

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

How do we update the value of an item in an array?

A
  • We access it using indexing and assign it to the new value
  • for example arrayName[0] = 4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

How do we iterate through every item in an array?

A
  • for i in arrayName {
    print(arrayName[i])
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What does the .sorted, .sort and .shuffle and .shuffled functions do?

A
  • .Sorted returns a sorted array, where as .sort modifies the actual array by sorting it
  • .Shuffled returns a shuffled array, where as .shuffle modifies the actual array by shuffling it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Is membership testing faster in arrays or sets?

A

It’s faster in sets (because items must be unique)

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

When we create a set what must we define?

A
  • if it’s empty upon initialisation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What fundamental set operations can we perform in swift on sets

A
  • intersection
  • symmetric difference
  • union
  • subtraction
  • equality, subset, superset, disjoint
49
Q

Why is any value we attempt to retrieve from a dictionary an optional

A
  • because we can’t determine whether it exists before testing.
50
Q

How do we check is a dictionary is empty?

A

we use the .isEmpty function

51
Q

how do we add a new element

A

we can just set up a new key value pair
- myDict[5] = “i miss my gf”
- we can use the same method to update values

52
Q

What happens when you print out an optional value?

A

it prints out the word “optional” as well

53
Q

How can we prevent it printing out the word “optional” as well for dictionaries with an optional type?

A

By providing a default, the value is no longer an optional and you can avoid it printing out “optional”

54
Q

How can you specify a default value for a dictionary in swift?

A

By writing the key : default : “bla”

55
Q

How do you create optional optionals?

A

By inserting a variable with the optional data type into another variable with the type optional
- for example putting an variable of type String? as the value in the key-value pair in a dictionary with type [String : String?]

56
Q

What is a countable range?

A
  • good for iterating over
  • range declared using ..< or …
57
Q

If you declare a type with a range what happens?

A
  • The complier tries to figure it
58
Q

What common incremental symbol is not accepted in swift

A

++

59
Q

What is a Stridable range?

A

Stridable means that the range is in a data type format that will allow you to step through the range in increments.

60
Q

What type of ranges are not stridable?

A

Floating point ranges because in swift they are no countable ranges, because we don’t know what to count up in

61
Q

What can we use to create a countable range from a floating point range?

A

a global method:
for y in stride(from: 0.3, to: 1.2, by 0.3) {print y}

62
Q

What function can we use to get a loop to step through a range in reverse order (backwards)

A

use the function .reversed()
-since we aren’t allowed to swap around the lower and upper bounds
- you don’t get the utmost bound using this method though!

63
Q

How do we loop in arrays?

A

for i in arrayA {print(i)}

64
Q

Why might we use arrays instead of sets if we want to iterate over them?

A

Due to the way sets store values - they may not always be stored in the order we see them in on-screen you can’t guarantee the order in which items from a set will be returned

65
Q

What happens when we iterate over an array?

A
  • each key-value pair is considered one item, so if we try to return i, we will get a tuple with the key and the value
  • we can deal with this of course by just naming the parts of the tuple (and discarding the key if we want)
66
Q

How do we ignore an element in a tuple if it’s not important to us?

A

We just give it an _ instead of a name
- for (user, _) in peopleInfo {print(user)}

67
Q

what does || and && mean in swift?

A
  • || stands for “OR” and && stands for “AND”
68
Q

What does == mean and != mean in swift?

A

a == b means if a is the same as b
a !=b means if a is not the same as b

69
Q

What does defining a class or structure in swift do?

A

It defines a brand new Swift type
- class someClass {}
- struct someStructure {}

70
Q

Why do we have both classes and structures?

A
  • When we pass a class to a method we are passing a pointer to it
  • However when we pass a structure to a method we pass a copy of it (the actual data)
  • Classes end up with a pointer to the same instance
71
Q

What is the foundation framework of swift?

A
  • Defines a base layer of classes for use in creating applications
  • provides wrappers for primitive types and collections
  • UIKit
72
Q

What does Cocoa touch include?

A

includes the foundation and UIkit frameworks

73
Q

What are the foundation classes that are bridged to from swifts standard types?

A
  • String - NSString
    Array - NSArray
    Set - NSSet
    Dictionary - NSDictionary
74
Q

Copying items to the disk takes a long time so what does swift do to save time?

A

only copies objects if you request to change them
End up with several different references to change it.

75
Q

When is an initialiser called?

A

when a new instance is created?

76
Q

What does the program do when it has a lot of computed values?

A

We can also have Computed properties.
- When we have multiple computed values there’s no point in storing all these values and using up memory so we just store the initial value then re-compute all these values again when we run the code.

77
Q

What is used to make code more readable with init() statements?

A

Initialisation parameters - define types and names of values to customise initialisation process

78
Q

What do you do if you don’t want to put the argument label as part of a Initialisation parameter?

A

if you don’t want one, you put “_” in the definition to make it an anonymous label

79
Q

What is true about the properties of a value inside a structure?

A

the properties of a value type (e.g. a Struct) cannot be modified from within its instance methods

80
Q

How can we modify the values of properties in a structure?

A

We mark the method as mutating

81
Q

What is the difference between classes and structures?

A
  • Classes lets you modify the values of properties inside it’s definition
  • Structures don’t let you modify values of properties inside its definition unless you specify mutating
82
Q

What is an enum (or enumeration)?

A

An object type whose instances represent distinct predefined alternative values/a list of related values.
- Think of the values as members

83
Q

What is a common use of enums?

A

Enums can be used often for making fonts in italics and bold

84
Q

Why is it useful that we assign strings to related values in Enums?

A

We can assign strings to the related values in Enums that have the correct name since for the variable name we can’t use specific symbols like th ‘-’ in ‘C-3PO’

85
Q

What does using enums ensure?

A

By using enums, the compiler ensures that you’re not referring to a value that doesn’t exist.

86
Q

What are protocols?

A

Protocols are like blueprints. If you conform to that blueprint, you adopt it’s properties and methods

87
Q

What does the protocol definition not include?

A

The body of the method

88
Q

in protocols, what are property requirements always declared as?

A

they’re always declared as vars
- also needs to specify if a property is gettable or settable

89
Q

What is a ternary conditional operator?

A

Ternary conditional operator: allows for if the statement evaluates to true then print the thing on the left otherwise print the thing on the right of the semicolon.

(in this case ? is does not mean optional type)

90
Q

What use are protocols?

A

They are a good way for defining a set of required functionality that other types can adopt

91
Q

What are gaurds?

A
92
Q

What can lead to the pyramid of doom?

A
  • lots and lots of nested if statements
93
Q

What are the two methods for avoiding the pyramid of doom?

A
  • invert the behaviour of the if statement by returning each time so that we don’t have to keep indenting the code. this works but it changes the logic which is confusing for us
  • using guards lets you keep the same logic but you don’t need to indent
94
Q

What can you often replace if statements with?

A

A gaurd

95
Q

What do we use to do error handling in swift?

A

try, catch and throw keywords

96
Q

What can you define to be used in the event of unexpected situations?

A
  • functions and methods that throw errors
97
Q

What is the syntax of a method (what does each method declaration consist of?)

A
  • the word func
  • a name (starts with lowercase letter)
  • an optional list of arguments
  • an optional return type
98
Q

By default parameters are named, how can we suppress this

A

We use an underscore instead of a name

99
Q

What does the keyword static mean?

A

Both Types and instances can have methods/properties

100
Q

How many different ways can we create instances if there are 4 init() functions within the class defintion?

A

4

101
Q

How can we replace multiple initialisers with one initialiser (if they take different arguments)?

A

can use default values in the attributes because then when the user inputs only some of the arguments, it’s fine but also if they input all of the arguments then they can just overwrite the default values

102
Q

What can the type AnyObject refer to?

A

AnyObject - can only refer to a Class

103
Q

What is casting?

A
  • because swift is strongly typed we need to change one item type into another sometimes to get extended functionality
104
Q

What are closures?

A
  • Self-contained blocks of code that can be passed around and used in your code
  • they can capture and store references to constants or variables
105
Q

What are the three kinds of closures?

A

*global functions - have a name & cannot capture any values
*nested functions - have a name & can capture values from their enclosing functions
*closure expressions - no name & can capture values from their context

106
Q

Give an example of a Swift standard library closure method?

A

sorted(by:)

107
Q

Why do we not need to use the keyword return in single expression closures?

A

Single expression closures can implicitly return the result of their single expression

108
Q

What are trailing closures?

A
  • If you need to pass a long closure expression to a function as it’s final argument, you can write it as a trailing closure.
109
Q

When do you not need to write the pair of parenthesis with a trailing closure?

A

If the closure is the only parameter to the method and it is provided as a trailing closure, then you don’t need to
write the pair of parentheses.

110
Q

What did apple traditionally use for memory management?

A

Some sort of Garbage Collection process to deal with formerly used blocks

111
Q

What happens in garbage collection for memory management?

A
  • an automated process runs at various times, to check which blocks can be deallocated, and return them to the general pool
  • Typically this involves tracing objects that are reachable via a set of root objects (and disposing of those that aren’t reachable)
112
Q

Why is garbage collection not a great memory management procedure?

A

GC can be time-consuming and require significant extra resources - leading to other processes pausing.
*Not good if you’re in the middle of a phone call!

113
Q

What are the two approaches to managing memory used in iOS?

A
  • ARC: Automatic Reference Counting
  • MRR: Manual Retain-Release
114
Q

Describe MRR: Manual Retain-Release

A

Developer explicitly inserts memory management calls (retain and release) into the code

115
Q

Describe ARC: Automatic Reference Counting

A

Compiler evaluates the requirements of your objects, and automatically inserts memory management calls at
compile time.

116
Q

When is an object alive and valid?

A
  • Every allocated object has a retain count
  • As long as retain count is > 0
117
Q

Describe ARC (Automatic Reference Counting)

A

*When you create an instance of a class, ARC also allocates a control block to maintain it
*When you are done with the instance, ARC frees up the memory it used
*But what if it deallocated an instance that actually was in use? - most probably your app would crash
*ARC tracks properties, constants and variables to check the
instance is not being referenced

118
Q

Why are strong reference cycles a problem?

A

Because they can lead to memory leaks

119
Q

How can we avoid strong reference cycles?

A

By declaring relationships as weak or unowned