【问题标题】:Youtube API to AlamoFire in SwiftSwift 中 AlamoFire 的 Youtube API
【发布时间】:2015-03-10 23:59:34
【问题描述】:

我的 Swift 代码有问题,其中包括框架 Alamofire 和 SwiftyJson。

我正在尝试通过 https://gdata.youtube.com/feeds/api/videos?q=kortsagt&max-re%E2%80%8C%E2%80%8Bsults=5&v=2&alt=jsonc&orderby=published 从 YouTube API 获取一些数据

并且代码运行良好,直到到达与

if let data = jsonObj.arrayValue as [JSON]?{}

然后就死在那里。

这是我的示例代码:

func loadNewVideo(){
    if(loadingStatus == false){
        loadingStatus == true
        dataVideo.removeAll()
        Alamofire.request(.GET, "https://gdata.youtube.com/feeds/api/videos?q=kortsagt&max-re%E2%80%8C%E2%80%8Bsults=5&v=2&alt=jsonc&orderby=published").responseJSON { (request, response, json, error) in
            //println(json)
            if json != nil {
                var jsonObj = JSON(json!)
                println(jsonObj)
                if let data = jsonObj.arrayValue as [JSON]? {
                    println(data)
                    dataVideo = data
                    self.collectionView?.reloadData()
                }
                else {
                    println("loading error")
                }
            }
        }
    }
    else{
        println("Loader JSON")
    }
}

这段代码有什么问题,我该如何解决?

【问题讨论】:

    标签: api swift youtube alamofire


    【解决方案1】:

    作为评论效果更好,但我没有声誉。在第一个答案中午结束时表示“您需要弄清楚如何将项目变成视频数据”。我只是希望补充一点。 Youtube会给你一个视频ID。您可以使用 3rd 方库(例如 this librarythis library)轻松播放视频,还有其他库,它们相当容易实现。

    【讨论】:

    • 为您的代表和您的答案提供大量资源
    【解决方案2】:

    您正在正确地调用 Alamofire,但您没有正确解析从服务器返回的数据。首先,我建议您通读 SwiftyJSON README。这应该让您对如何在较高级别使用 SwiftyJSON 有一个很好的理解。然后尝试使用以下修改。

    func loadNewVideo(){
        if (loadingStatus == false) {
            loadingStatus == true
            dataVideo.removeAll()
    
            let URLString = "https://gdata.youtube.com/feeds/api/videos?q=kortsagt&max-re%E2%80%8C%E2%80%8Bsults=5&v=2&alt=jsonc&orderby=published"
    
            let request = Alamofire.request(.GET, URLString)
            request.responseJSON { request, response, jsonData, error in
                if let json = json {
                    let json = JSON(jsonData)
                    if let items = json["data"]["items"].array {
                        println(items)
    
                        for item in items {
                            println(item)
                        }
    
                        // You need to parse the items into the video data
                        dataVideo = data
                        self.collectionView?.reloadData()
                    }
                } else {
                    println("loading error")
                }
            }
        } else {
            println("Loader JSON")
        }
    }
    

    它不完整。您需要弄清楚如何将项目转换为视频数据。我会把那部分留给你。

    【讨论】:

    • 嘿@Joachimdj,这最终解决了您的问题吗?如果是,您可以投票并标记为答案吗?
    • 太棒了!然后你应该将其标记为正确答案并投票。
    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2019-05-17
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    相关资源
    最近更新 更多