如果Index超出范围,请继续下一步结果SwiftyJSON& AlamoFire

时间:2018-05-05 05:18:24

标签: swift alamofire swifty-json

我目前有一个文件名列表,我从Youtube的JSON文件中插入Alamofire.request以最终.append "videoId"。当我将搜索查询硬编码到我知道会导致视频结果的列表中时,它的效果非常好。但不幸的是,列表中的某些文件名找不到任何视频。这导致fatal error: Index out of range我希望找到一种方法,以便在"videoId"无效时继续使用列表中的下一个项目。我已尝试过elsecontinue等,但却永远无法做到。所以这个例子没有尝试迭代错误的搜索

我对编码很陌生,所以如果我完全离开,请随时改变。

这是我的代码。这是我的函数内部,它也获取我想要的文件名并将它们插入[String]

for i in testList {

    Alamofire.request("https://www.googleapis.com/youtube/v3/search?key=\(API_Key)&part=snippet&q=\(i)%20Music%20Video&maxResults=1&type=video&order=relevance").responseJSON { (response)  in

        print("Result: \(response.result)")
            var json = JSON(response.result.value as Any)
        print(json)

        if let _ = json["items"].array{


                    if json["items"].arrayValue[0]["id"].exists(){
                        let songPath = json["items"].arrayValue[0]["id"]
                        let videoIdPath = songPath["videoId"].stringValue
                        var arrayOfVideos = ""


                        arrayOfVideos.append(videoIdPath)
                        print(arrayOfVideos)
                        videoArray += [arrayOfVideos] }

这是关于上下文的YouTube请求回复

Result: SUCCESS
{
  "items" : [
    {
      "etag" : "\"95M1zlW0txkV42I4OG1Zscxrg5A\/TwSmd4IGGf1uBi-y10rCaZ53JsU\"",
      "kind" : "youtube#searchResult",
      "id" : {
        "kind" : "youtube#video",
        "videoId" : "9jfhlttiQws"
      },
      "snippet" : {
        "thumbnails" : {
          "default" : {
            "url" : "https:\/\/i.ytimg.com\/vi\/9jfhlttiQws\/default.jpg",
            "width" : 120,
            "height" : 90
          },
          "high" : {
            "url" : "https:\/\/i.ytimg.com\/vi\/9jfhlttiQws\/hqdefault.jpg",
            "width" : 480,
            "height" : 360
          },
          "medium" : {
            "url" : "https:\/\/i.ytimg.com\/vi\/9jfhlttiQws\/mqdefault.jpg",
            "width" : 320,
            "height" : 180
          }
        },
        "channelId" : "UCdWVO7O7stk7MNKVARRV8rw",
        "title" : "LUAM - Clockwork Indigo (The Flatbush ZOMBiES + The Underachievers) (LYRICS)",
        "publishedAt" : "2014-10-19T01:37:31.000Z",
        "description" : "LUAM by clockwork Indigo consisting of Beast Coast rap groups Flatbush ZOMBiES & The Underachiever off of their collaborative Debut Mixtape \"Clockwork ...",
        "liveBroadcastContent" : "none",
        "channelTitle" : "JayMendezTv"
      }
    }
  ],
  "kind" : "youtube#searchListResponse",
  "nextPageToken" : "CAEQAA",
  "regionCode" : "US",
  "pageInfo" : {
    "resultsPerPage" : 1,
    "totalResults" : 10073
  },
  "etag" : "\"95M1zlW0txkV42I4OG1Zscxrg5A\/KIryoSi8eZnM0UdfR0ZqyaHGB_k\""
}
9jfhlttiQws

1 个答案:

答案 0 :(得分:0)

没有人回答我的问题,但幸运的是我自己找到了解决方案。如果未找到Youtube结果,"items"数组将为空,因为找不到"videoId"。所以我所要做的就是在代码中添加if语句,如此

  if let items = json["items"].array{
if items.isEmpty{
                    return
                }
                else {