Jun 14, 2024
{key: value}
pairs{
"course": "cs50",
"tracks": ["mobile", "web", "games"],
"year": 2019
}
do {
let result = try someFunction()
} catch let error {
print(error)
}
let reversed = names.sorted { (s1: String, s2: String) -> Bool in
return s1 > s2
}
https://pokeapi.co/api/v2/
URL
and URLSession
classeshttps://pokeapi.co/api/v2/pokemon?limit=151
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()
Codable
protocolstruct Pokemon: Codable {
let name: String
let url: String
}
struct PokemonList: Codable {
let results: [Pokemon]
}
do-try-catch
to handle decoding errorsDispatchQueue.main.async {
self.tableView.reloadData()
}
struct PokemonType: Codable {
let name: String
let url: String
}
struct PokemonTypeEntry: Codable {
let slot: Int
let type: PokemonType
}
struct PokemonData: Codable {
let id: Int
let types: [PokemonTypeEntry]
}