【发布时间】:2018-01-05 19:39:42
【问题描述】:
我正在使用 YouTube 数据 API 和 Alamofire 来显示我的 YouTube 频道的视频,并且它们会动态更新。这是我的代码:
func getFeedVideo() {
Alamofire.request("https://www.googleapis.com/youtube/v3/playlists", parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
if let JSON = response.result.value {
if let dictionary = JSON as? [String: Any] {
var arrayOfVideos = [Video]()
for video in dictionary["items"] as! NSArray {
// Create video objects off of the JSON response
let videoObj = Video()
videoObj.videoID = (video as AnyObject).value(forKeyPath: "snippet.resourceId.videoId") as! String
videoObj.videoTitle = (video as AnyObject).value(forKeyPath: "snippet.title") as! String
videoObj.videoDescription = (video as AnyObject).value(forKeyPath: "snippet.description") as! String
videoObj.videoThumbnailUrl = (video as AnyObject).value(forKeyPath: "snippet.thumbnails.maxres.url") as! String
arrayOfVideos.append(videoObj)
}
self.videoArray = arrayOfVideos
if self.delegate != nil {
self.delegate!.dataReady()
}
}
}
}
}
我遇到了一个错误
线程 1:EXC_BAD_INSTRUCTION
在线for video in dictionary["items"] as! NSArray {。在控制台中,我看到了
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
数据显示在UITableView 中。有关如何解决此问题的任何想法?
【问题讨论】:
标签: ios swift youtube alamofire youtube-data-api