Hi I'm having an issue with parsing.
When I try to parse this JSON in my tableviewcell and can't get the values that I need.
My request:
let videos = NewestVideos()
let URL = "https://api.vid.me/channel/1/new"
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, self.URL).responseObject { (response: Response<NewestVideos, NSError>) in
switch response.result {
case .Success(self.videos):
print("")
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! NewTableViewCell
cell.newVideoNameLabel.text = resultr
return cell
}
How do I get values in tableviewcell, and make the correct request?
My mapping via ObjectMapper:
class Videos: Mappable {
var embedUrl: String?
var title: String?
required init?(_ map: Map){
}
func mapping(map: Map) {
embedUrl <- map["embed_url"]
title <- map["title"]
}
}
class NewestVideos: Mappable {
var videos: [Videos]? = []
required init?(_ map: Map){
}
func mapping(map: Map) {
videos <- map["videos"]
}
}
Your mappable classes look okay, apart from Videos should be Video.
class Video: Mappable {
var embedUrl: String?
var title: String?
required init?(_ map: Map) {}
func mapping(map: Map) {
embedUrl <- map["embed_url"]
title <- map["title"]
}
}
class NewestVideos: Mappable {
var videos: [Video]? = []
required init?(_ map: Map) {}
func mapping(map: Map) {
videos <- map["videos"]
}
}
Next in your controller, you were doing something weird with your switch statement. You should be letting the response result as .Some
and then setting self.videos
to the newest.videos
.
class ViewController: UIViewController, ... {
...
var videos = [Video]() {
didSet {
tableView.reloadData()
}
}
...
override func viewDidLoad() {
super.viewDidLoad()
let request = Alamofire.request(.GET, "https://api.vid.me/channel/1/new")
request.responseObject { (response: Response<NewestVideos, NSError>) in
switch response.result {
case .Success(let newest):
self.videos = newest.videos
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}
...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! NewTableViewCell
let video = self.videos[indexPath.row]
cell.newVideoNameLabel.text = video.name
return cell
}
...
}