我正在使用YouTube數據API和Alamofire來顯示我的YouTube頻道的視頻並動態更新。這裏是我的代碼:使用YouTube數據API和Alamofire時出錯
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
中。有想法該怎麼解決這個嗎?
我收到一個錯誤'int類型對線沒有下members''如果讓視頻ID =視頻[「ID」]作爲?字符串{','如果讓snippet = video [「snippet」] as? [字符串:任何] {',和'如果讓縮略圖=視頻[「縮略圖」]? [字符串:任何] {' –
請參閱我編輯的答案。雖然爲時已晚。 –