Swift Flashcards
Define property wrappers
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.
What steps would you follow to make a network request?
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!
What’s Codable?
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.
What are the steps to call an api and convert the JSON?
- Create a URL based on the url string (guard let url = URL(string: urlString) { }
- Call the api with the URLSession
URLSession.shared.dataTask(with: url) { data, response, error in
// Decode JSON
}.resume()
How to make a struct be able to be sorted based on the property you selected.
- Add Comparable protocol
- Create a function that sorts it based on the property you decide on.
static func < (lhs: Page, rhs: Page) -> Bool {
lhs.title < rhs.title
}
What are the options when saving data on an iOS App
- UserDefaults
- Write to document file
- Core Data
- Cloudkit?
What are the steps to saving data from a file?
- Get the url for the app: FileManager.default.urls(for: in:)
- Append the path component (.appendingPathComponent(“fileName”))
- Create Data object: Data(ContentsOf:)
- try to decode the data: JSONDecoder().decode(_: from:)
What are the steps to writing data to a file
- Get the documents directory url and append a path component to it, aka file name: FileManager.default.urls(for: , in: )
- Create file name by appending a path name: .appendingPathComponent(“SavedPlaces”)
- Encode the data to be saved: JSONEncoder().encode(data)
- Write the data to the file: data.write(to: options:)