Swift Flashcards

1
Q

Define property wrappers

A

a property wrapper is essentially a type that wraps a given value in order to attach additional logic to it — and can be implemented using either a struct or a class by annotating it with the @propertyWrapper attribute.

Besides that, the only real requirement is that each property wrapper type should contain a stored property called wrappedValue, which tells Swift which underlying value that’s being wrapped.

Property wrappers can also have properties of their own, which enables further customization, and even makes it possible to inject dependencies into our wrapper types.

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

What steps would you follow to make a network request?

A

Suggested approach: There are so many ways of answering this (not least “use Alamofire”), but the main thing is to demonstrate that you know it needs to be asynchronous to avoid blocking the main thread. Don’t forget to mention the need to push work back to the main thread when it’s time to update the user interface!

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

What’s Codable?

A

A type that can convert itself into and out of an external representation.

Codable is a type alias for the Encodable and Decodable protocols. When you use Codable as a type or a generic constraint, it matches any type that conforms to both protocols.

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

What are the steps to call an api and convert the JSON?

A
  1. Create a URL based on the url string (guard let url = URL(string: urlString) { }
  2. Call the api with the URLSession
    URLSession.shared.dataTask(with: url) { data, response, error in
    // Decode JSON
    }.resume()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to make a struct be able to be sorted based on the property you selected.

A
  1. Add Comparable protocol
  2. Create a function that sorts it based on the property you decide on.
    static func < (lhs: Page, rhs: Page) -> Bool {
    lhs.title < rhs.title
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the options when saving data on an iOS App

A
  1. UserDefaults
  2. Write to document file
  3. Core Data
  4. Cloudkit?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the steps to saving data from a file?

A
  1. Get the url for the app: FileManager.default.urls(for: in:)
  2. Append the path component (.appendingPathComponent(“fileName”))
  3. Create Data object: Data(ContentsOf:)
  4. try to decode the data: JSONDecoder().decode(_: from:)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the steps to writing data to a file

A
  1. Get the documents directory url and append a path component to it, aka file name: FileManager.default.urls(for: , in: )
  2. Create file name by appending a path name: .appendingPathComponent(“SavedPlaces”)
  3. Encode the data to be saved: JSONEncoder().encode(data)
  4. Write the data to the file: data.write(to: options:)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly