Flipturn Technical Flashcards

1
Q

Steps to read json from file?

A
  • add file.json to directory
  • create Codable struct for said data
  • read data
  • decode data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you read a local json file?

A

let url = Bundle.main.url(forResource: “name”, withExtension: “json”)

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

How do you get the data from a url?

A

let data = Data(contentsOf: url)

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

How would you create Codable structs for this json?

{
“company”: {
“name”: “ABC Corp”,
“location”: “New York”
}
}

A

Create a “Model” type at the top level. and the model type has a “company” property, and the company type has a “name” property

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

What is the line to decode some json?

A

let model = decoder.decode(Model.self, from: data)

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

List 2 ways to decode snake case json

A

use coding keys or tell the decoder to expect snake case

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

How do you decode snake case with coding keys?

A

the codable struct needs an enum that conforms to CodingKey

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

Decode this json:

{
“company”: {
“legal_name”: “ABC Corp”,
“location”: “New York”
}
}

A

Create a “Model” type, then create a “Company” type. And Company should have an enum that conforms to “CodingKey”

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

Steps to read json from string

A

same as from file except the Data initializer uses string.utf8, ie:

let data = Data(string.utf8)

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