【问题标题】:YouTube API/JSON/SwiftYouTube API/JSON/Swift
【发布时间】:2018-06-27 18:49:26
【问题描述】:

在我似乎无法弄清楚的事情上需要您的帮助。

我正在尝试通过 API/JSON 显示我当前的 Youtube 视频,当我去构建项目并单击假设在我的视图控制器中构建视频的选项卡时,我在以下位置收到错误:

“线程 1:致命错误:在展开可选值时意外发现 nil”

它突出显示:

对于字典中的项目[“项目”] 为! NSArray { 打印(项目)

已满:

import UIKit
import Alamofire

protocol VideoModelDelegate {
    func dataReady()
}

class VideoModel: NSObject {

    var videoArray = [Video]()

    var delegate:VideoModelDelegate?

    func getFeedVideos() {

        let API_KEY = "AIzaSyCvsoQ-MffdpB8ZPw1SSZiS36BbRUMp2cs"
        let UPLOADS_PLAYLIST_ID = "TLGG_vHch6CEugIxNzAxMjAxOA"
        let CHANNEL_ID = "UC2D6eRvCeMtcF5OGHf1-trw"

        // fetching the videos dinamycally through API
        Alamofire.request("https://www.googleapis.com/youtube/v3/playlistItems", method: .get, parameters: ["part":"snippet,contentDetails", "playlistId":UPLOADS_PLAYLIST_ID, "key":API_KEY, "channelId":CHANNEL_ID], encoding: URLEncoding.default, headers: nil).responseJSON{ (response) in

            if let JSON = response.result.value {

                var arrayOfVideos = [Video]()

                if let dictionary = JSON as? [String:Any]{

                    for item in dictionary["items"] as! NSArray {
                     print(item)

                        //Create video objects off the JSON response
                        let videoObj = Video()
                        videoObj.videoId = Video.value(forKeyPath: "snippet.resourceID.videoId") as! String
                        videoObj.videoTitle = Video.value(forKeyPath: "snipper.tittle") as! String
                        videoObj.videoDiscription = Video.value(forKeyPath: "snippet.description") as! String
                        videoObj.videoThumbnailURL = Video.value(forKeyPath: "snippet.thumbnails.maxres.url") as! String

                        arrayOfVideos.append(videoObj)
                    }}
                    // When all the video objects have been constucted, assign the array to the VideoModel property
                    self.videoArray = arrayOfVideos

                    //Notify the delegate that the data is ready
                    if self.delegate != nil{
                        self.delegate!.dataReady()
                    }
                }

            }

        }

    func getVideos() -> [Video] {

        //Create a empty array of video objects
        var videos = [Video]()

        // Create a video object
        let video1 = Video()

        //Assign properties
        video1.videoId = "h8onY49kJPo"
        video1.videoTitle = "5-24-11 Canton Lake, Ok longdale tornado EF-3 Blaine County wedge"
        video1.videoDiscription = "***NOT FOR BROADCAST***"

        //Append in into  the videos array
        videos.append(video1)


        // Create a video object
        let video2 = Video()

        //Assign properties
        video2.videoId = "bAM9njN1Llo"
        video2.videoTitle = "8-25-2017 Fulton, TX Hurricane Harvey Extreme, insane incredible wind from eye wall"
        video2.videoDiscription = "***NOT FOR BROADCAST***"

        //Append in into  the videos array
        videos.append(video2)


        // Create a video object
        let video3 = Video()

        //Assign properties
        video3.videoId = "od79-MRWVmA"
        video3.videoTitle = "12-12-2017 Erie, Pa Lake Effect Snow Warning, Historic downtown, heavy snow piling up I-90"
        video3.videoDiscription = "***NOT FOR BROADCAST***"

        //Append in into  the videos array
        videos.append(video3)


        // Create a video object
        let video4 = Video()

        //Assign properties
        video4.videoId = "NxOj10ODtcI"
        video4.videoTitle = "6-15-2017 Great Bend, Ks Gustnado, dust storm, severe thunderstorm"
        video4.videoDiscription = "***NOT FOR BROADCAST***"

        //Append in into  the videos array
        videos.append(video4)


        // Create a video object
        let video5 = Video()

        //Assign properties
        video5.videoId = "vk5o-KSQf9I"
        video5.videoTitle = "3-1-17-Perryville-Mo-tornado-damage-I-55"
        video5.videoDiscription = "***NOT FOR BROADCAST***"

        //Append in into  the videos array
        videos.append(video5)

        //Return the array to the caller
        return videos
    }

}

我希望这有助于解释我的问题。仅仅学习了大约一年半的时间(我自己),对编码还是很陌生。

如果我的帖子有错误,我深表歉意,因为我是该小组的新手。

【问题讨论】:

  • 显然 dictionary["items"] 要么是 nil 要么不是数组。
  • 还是比较新的,那我要怎么改呢?

标签: ios json swift xcode youtube


【解决方案1】:

好吧,看来dictionary["items"] 不是一个数组。尝试使用“guard”语句而不是“if”。调试会更好

【讨论】:

  • 您能详细说明一下它的外观吗?
  • @JoshNapper 您可以在“控制流”部分的官方文档中查看它here
  • 所以我假设这不是一个简单的解决方案?哈哈
  • 我查看了你发送的内容,但我仍然迷路:(
  • @JoshNapper 例如if let JSON = response.result.value,您可以用guard let JSON = response.result.value else{/*if response.result.value is nil than compiler comes here, if no it moves forward*/} 之类的守卫替换。并且在断点的帮助下,您的代码将更容易理解,并且不会有那么多继承的“if”语句和括号
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 1970-01-01
  • 2016-01-17
  • 2018-03-09
  • 2019-05-17
  • 2018-03-14
  • 2011-11-30
相关资源
最近更新 更多