Description: a simple movie app that demonstrate how to get JSON data and dynamically create the corresponding class. Source Code
class Movie: NSObject {
var id: Int = 0
var originalTitle = ""
var releaseDate = ""
var posterPath = ""
var title = ""
var voteAverage:Float = 0
var voteCount:Int = 0
func getPosterURL() -> String {
return ALFrameworkConfig.TMDBImageURL + posterPath
}
}
import UIKit
import ALFramework
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var dataSource:TableViewDataSource!
var movies:[Movie] = []
override func viewDidLoad() {
super.viewDidLoad()
dataSource = TableViewDataSource(items: movies, cellIdentifier: "cell", cellActionHandler: nil, configureBlock: { (cell, item) -> () in
if let actualCell = cell as? MovieTableViewCell {
actualCell.configCells(item as! Movie)
}
})
tableView.dataSource = dataSource
tableView.delegate = dataSource
let url = ALFrameworkConfig.TMDBURLPopular + ALFrameworkConfig.TMDBKey
ALUtils.getJSONObjectFromURL(url, callback: { (jsonData) -> Void in
if let jsonArr = jsonData["results"] as? NSArray {
for jsonDictData in jsonArr {
if let jsonDict = jsonDictData as? NSDictionary {
// dynamically create movie class
let movie = Movie.fromJSON(jsonDict)
self.movies.append(movie)
}
self.dataSource.items = self.movies
self.tableView.reloadData()
}
}
})
}
}
Links: