-1

I'm simply fetching data from youtube API and show it on tableView. It was working fine just before Swift 3 upgrade but now it gives me an error.

I'm able to print JSON successfully but cannot have the title, description and the imageView displayed.

When I click on the videoTableView, I get an error on the lines starting with "videoObj".

The error message:

this class is not key value coding-compliant for the key snippet.'

let videoObj = Video()
videoObj.videoId = Video.value(forKeyPath: "snippet.resourceId.videoId") as! String
videoObj.videoTitle = Video.value(forKeyPath: "snippet.title") as! String
videoObj.videoDescription = Video.value(forKeyPath: "snippet.description") as! String
videoObj.videoThumbnailUrl = Video.value(forKeyPath: "snippet.thumbnails.maxres.url") as! String

arrayOfVideos.append(videoObj)

if self.delegate != nil {
    self.delegate!.dataReady()
}

I've looked at similar questions and some of them suggested to check the storyboard for missing connections or class name but couldn't replicate them on my own project.

Some also suggested to clean the project, delete the app from simulator and device and get rid of the derived data but none of them seems to be the solution for me.

Before the Swift 3 upgrade, the lines were as follows;

let videoObj = Video()
videoObj.videoId = video.valueForKeyPath("snippet.resourceId.videoId") as! String
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What is `Video`? The error indicates that it doesn't have a property named `snippet`. – rmaddy Oct 09 '16 at 17:30
  • Why is the Swift 3 code calling `... = Video.value...` while the older Swift code is calling `... = video.value...`? Note `Video` vs `video`. – rmaddy Oct 09 '16 at 17:32
  • 1
    @matt This has nothing to do with storyboards or that other question. This is an actual KVC issue. – rmaddy Oct 09 '16 at 17:33
  • @rmaddy Video is a class with the following variables; class Video: NSObject { var videoId: String = "" var videoTitle: String = "" var videoDescription: String = "" var videoThumbnailUrl: String = "" video on the other side is part of an array; var arrayOfVideos = [Video]() if let items = JSON["items"] as? [[String : Any]] { for video in items { let videoObj = Video() When I use video instead of Video, I get: Value of type 'Dictionary' has no member 'value' – Burak Pusat Oct 09 '16 at 17:55

1 Answers1

0

Where you have Video, put (video as NSDictionary).

So for example:

videoObj.videoId = (video as NSDictionary).value(forKeyPath: "snippet.resourceId.videoId") as! String

...and so on.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks, @matt . With your suggestion, I was able to get rid of the problem. But this time, it moved into a different section where I created a datatask and pass in the request( as! (Data?, URLResponse?, Error?) -> Void) ) with a different error message "(lldb) ". Thanks again. – Burak Pusat Oct 10 '16 at 08:20