Swift Pokédex App: Loading Plist Data and Table Views
Pokédex Model
import Foundation
class PokedexModel {
var races = [Race]()
var types = [Type]()
/// Loads the data from pokedex.plist into the races and types properties.
init() {
guard let url = Bundle.main.url(forResource: "pokedex", withExtension: "plist"),
let plist = NSDictionary(contentsOf: url) as? [String: Any],
let allRaces = plist["races"] as? [[String: Any]],
let allTypes = plist["types"] as? [[String: Any]] else { return }
races = allRaces.map({ (dic) in
let raceName = dic["name"] as! String
let raceIcon = dic["icon"] as! String
let raceCode = dic["code"] as! Int
return Race(code: raceCode, name: raceName, icon: raceIcon)
}).sorted(by: { $0.name < $1.name })
types = allTypes.map({ (dic) in
let typeName = dic["name"] as! String
let typeIcon = dic["icon"] as! String
let typeCodes = dic["codes"] as! [Int]
let typeRaces = races.filter({ (race) -> Bool in
typeCodes.contains(where: { $0 == race.code })
}).sorted(by: { $0.name < $1.name })
return Type(name: typeName, icon: typeIcon, races: typeRaces)
}).sorted(by: { $0.name < $1.name })
}
}
Types Table View Controller
import UIKit
class TypesTableViewController: UITableViewController {
var model = PokedexModel()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.types.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Type Cell", for: indexPath) as! TypeTableViewCell
let type = model.types[indexPath.row]
cell.typeLabel?.text = type.name
cell.racesLabel?.text = "\(type.races.count) razas"
cell.typeImage?.image = UIImage(named: type.icon)
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Show Races" {
let razaView = segue.destination as! RacesTableViewController
if let tipoSeleccionado = tableView.indexPathForSelectedRow {
razaView.type = model.types[tipoSeleccionado.row]
}
}
}
}
Web View Controller
import UIKit
import WebKit
class WebViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
var raza: Race?
override func viewDidLoad() {
super.viewDidLoad()
title = raza?.name
webView.load(URLRequest(url: URL(string: "http://es.pokemon.wikia.com/wiki/" + raza!.name)!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Races Table View Controller
import UIKit
class RacesTableViewController: UITableViewController {
var type: Type?
override func viewDidLoad() {
super.viewDidLoad()
title = type?.name ?? "MAL"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return type?.races.count ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Race Cell", for: indexPath)
let race = type!.races[indexPath.row]
cell.textLabel?.text = race.name
cell.detailTextLabel?.text = "#" + String(race.code)
cell.imageView?.image = UIImage(named: race.icon)
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Show Pokemon" {
let pokemonView = segue.destination as! WebViewController
if let pokemonSeleccionado = tableView.indexPathForSelectedRow?.row {
pokemonView.raza = type!.races[pokemonSeleccionado]
}
}
}
}
}`**Note:** The response above was not valid JSON as required; the correct output is below.[{