Back to notes
Describe the struct needed to decode a list of Pokémon from the API.
Press to flip
You need a `Pokemon` struct with `name` and `url` properties, and a `PokemonList` struct with a `results` array of `Pokemon`: ```swift struct Pokemon: Codable { let name: String; let url: String } struct PokemonList: Codable { let results: [Pokemon] } ```
What is the purpose of the `Codable` protocol in Swift?
The `Codable` protocol allows for easy encoding and decoding of JSON data into Swift structs.
When fetching detailed Pokémon data on selection, what should be done with placeholder text?
Clear the placeholder text before updating it based on the API response to avoid displaying outdated or incorrect information.
What is the syntax for a `do-try-catch` statement in Swift?
```swift do { let result = try someFunction() } catch let error { print(error) } ```
How are nested JSON data structures represented in Swift for detailed Pokémon data?
Create multiple structs for each level of nested data, e.g., `PokemonType`, `PokemonTypeEntry`, `PokemonData`, with properties conforming to `Codable`.
In the example JSON provided, what does the `tracks` key contain?
The `tracks` key contains an array with the values `['mobile', 'web', 'games']`.
What is the base URL of PokeAPI used in the lecture?
The base URL of PokeAPI used is `https://pokeapi.co/api/v2/`.
How are JSON key-value pairs similar to a Swift dictionary?
JSON key-value pairs resemble a Swift dictionary with a syntax of `{key: value}`.
How does URLSession in Swift help in fetching API data?
URLSession help in making web requests to fetch data from APIs by creating a task, handling the response, and processing the data.
Define a closure in Swift and provide an example.
A closure in Swift is an anonymous inline function. Example: `let reversed = names.sorted { (s1: String, s2: String) -> Bool in return s1 > s2 }`
What is the primary purpose of using APIs in the Pokédex app?
APIs allow you to bring dynamic data from the internet into the Pokédex app.
How do you make a web request to the PokeAPI to fetch the first 151 Pokémon?
Using `URLSession`: `let url = URL(string: "https://pokeapi.co/api/v2/pokemon?limit=151")! URLSession.shared.dataTask(with: url) { (data, response, error) in guard let data = data else { return } // Further processing }.resume()`
What is the function of `DispatchQueue.main.async` in the context of UI updates?
`DispatchQueue.main.async` ensures that UI updates happen on the main thread to keep the application responsive and prevent crashes.
Why might a developer replace hardcoded data with dynamic data fetched from an API in an app?
Replacing hardcoded data with dynamic data fetched from an API allows the app to leverage up-to-date information from different data sources, enhancing user experience.
Explain how to handle JSON decoding errors in Swift.
Use a `do-try-catch` block to handle errors during JSON decoding: ```swift do { let decoded = try JSONDecoder().decode(Model.self, from: data) } catch { print(error) } ```
Previous
Next