Data Flashcards

1
Q

How is a dictionary different from an array?

A

It’s all down to how you access data: arrays must be accessed using the index of each element, whereas dictionaries can be accessed using something you define – strings are very common. Make sure and give practical examples of where each would be used.

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

What are the main differences between classes and structs in Swift?

A

Your answer ought to include a discussion of value types (like structs) and reference types (like classes), but also the fact that classes allow inheritance.
For bonus points you could mention that classes have deinit() methods and structs do not.

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

What are tuples and why are they useful?

A

Tuples are a bit like anonymous structs, and are helpful for returning multiple values from a method in a type-safe way, among other things. Make sure you go on to provide some explanation of where they might be useful, such as returning two values from an array.

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

What does the Codable protocol do?

A

This protocol was introduced in Swift 4 to let us quickly and safely convert custom Swift types to and from JSON, XML, and similar.

talk about customization points such as key and date decoding strategies, the CodingKey protocol, and more, so that you’re able to show you can handle a range of input and output styles.

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

What is the difference between an array and a set?

A

sets can’t contain duplicates and are unordered, so lookup is significantly faster. Note: this might sound like a trivial question, but the “significantly faster” part is critical – sets can be thousands of times faster than arrays depending on how many elements they contain. If you can, go on to give specific examples of where a set would be a better idea than an array.

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

What’s the importance of key decoding strategies when using Codable?

A

“key decoding strategies let us handle difference between JSON keys and property names in our Decodable struct” – then provide some kind of practical sample. For example, you might say that it’s common for JSON keys to use snake_case for key names, whereas in Swift we prefer camelCase, so we need to use a key decoding strategy to convert between the two.

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

When using arrays, what’s the difference between map() and compactMap()?

A

Remember to give practical examples as well as outlining the core differences. So, you might start by saying the map() transforms a sequence using a function we specify, whereas compactMap() does that same step but then unwraps its optionals and discards any nil values. For example, converting an array of strings into integers works better with compactMap(), because creating an Int from a String is failable.

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

Why is immutability important?

A

Immutability is baked deep into Swift, and Xcode even warns if var was used when let was possible. It’s important because it’s like a programming contract: we’re saying This Thing Should Not Change, so if we try to change it the compiler will refuse.

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

What are one-sided ranges and when would you use them?

A

So, you might say that one-sided ranges are ranges where you don’t specify the start or end of the range, meaning that Swift will automatically make the range start from the start of the collection or the end of the collection. They are useful when you want to read from a certain position to the end of a collection, such as if you want to skip the first 10 users in an array.

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

What does it mean when we say “strings are collections in Swift”?

A

This statement means that Swift’s String type conform to the Collection protocol, which allows us to loop over characters, count how long the string is, map the characters, select random characters, and more.

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

What is a UUID, and when might you use it?

A

UUID stands for “universally unique identifier”, which is a long string of hexadecimal numbers stored in a single type.

UUIDs are helpful for ensuring some value is guaranteed to be unique, for example you might need a unique filename when saving something.

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

What’s the difference between a value type and a reference type?

A

The best way to frame this discussion is likely to be classes vs structs: an instance of a class can have multiple owners, but an instance of a struct cannot.

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

When would you use Swift’s Result type?

A

Start with a brief introduction to what Result does, saying that it’s an enum encapsulating success and failure, both with associated values so you can attach extra information. I would then dive into the “when would you use it” part of the question – talking about asynchronous code is your best bet, particularly in comparison to how things like URLSession would often pass both a value and an error even when only one should exist at a time.

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

What is type erasure and when would you use it?

A

Type erasure allows us to throw away some type information, for example to say that an array of strings is actually just AnySequence – it’s a sequence containing strings, but we don’t know exactly what kind of sequence.

This is particularly useful when types are long and complex, which is often the case with Combine. So, rather than having a return type that is 500 characters long, we can just say AnyPublisher – it’s a publisher that will provide SomeType and never throw an error, but we don’t care exactly what publisher it is

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