【问题标题】:Error when Using YouTube Data API and Alamofire使用 YouTube 数据 API 和 Alamofire 时出错
【发布时间】: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


    【解决方案1】:

    这意味着您的字典中的项目没有值,或者您访问不正确。

    【讨论】:

      【解决方案2】:

      您正在尝试对 NSArray 进行强制强制转换。如果 dictionary["items"] 不是 NSArray,这将使您的应用程序崩溃。
      我建议您在循环之前放置一个断点以检查您的dictionary["items"] 的类型。

      示例:

      guard let items = dictionary["items"] as? NSArray else { return }
      

      【讨论】:

        【解决方案3】:

        请不要使用强制类型转换。它可能会导致您的应用程序崩溃。始终使用 if let 或 guard let。尝试像这样迭代它

        if let dictionary = JSON as? [String: Any] {
        var arrayOfVideos = [Video]()
        if let playlist = dictionary["items"] as? [Any] {
        
            for i in 0..<playlist.count {
        
                let videoObj = Video()
                if let video = playlist[i] as? [String: Any] {
                    if let videoId = video["id"] as? String {
                        videoObj.videoID = videoId
                    }
        
                    if let snippet = video["snippet"] as? [String: Any] {
                        if let videoTitle = snippet["title"] as? String {
                            videoObj.videoTitle = videoTitle
                        }
        
                        if let videoDescription = snippet["description"] as? String {
                            videoObj.videoDescription = videoDescription
                        }
                    }
        
                    if let thumbnails = video["thumbnails"] as? [String: Any]{
                        if let maxres = thumbnails["maxres"] as? [String: Any] {
                            if let url = maxres["url"] as? String {
                                videoObj.videoThumbnailUrl = url
                            }
                        }
                    }
                    arrayOfVideos.append(videoObj)
                }
            }
        }
        

        }

        【讨论】:

        • 我在if let videoId = video["id"] as? String {、if let snippet = video["snippet"] as? [String: Any] { 和if let thumbnails = video["thumbnails"] as? [String: Any]{ 行收到错误Type Int has no subscript members
        猜你喜欢
        • 2016-06-02
        • 1970-01-01
        • 2014-09-10
        • 2020-08-09
        • 2018-10-28
        • 2021-03-02
        • 2015-12-10
        • 1970-01-01
        相关资源
        最近更新 更多