2016-02-11 74 views
0

代碼波紋管工作正常,至少在我的幾個測試。問題是,我真的不知道我是否覆蓋了代碼中的每一個可能的失敗,或者如果這是處理多個「如果讓...」選項的最佳方法。有沒有更好更實際的方法來做到這一點?該代碼僅用於處理我從Youtube API獲取的數據。斯威夫特 - 解析多個選項Json答案

func requestPlaylistTrailers(){ 
     let playlist = self.playlistIdTrailers 
     self.delegate?.youtubeTrailersWillUpdate() 
     self.req(playlist) { (myData) -> Void in 
      var title = "" 
      var videoId = "" 
      var thumbnail = "" 
      if myData != nil{ 
       let data = myData! 
       let items = data["items"] as! Array<AnyObject> 
       for item in items { 
        //print(item) 
        if let itemDict = item as? Dictionary<NSObject,AnyObject>{ 
         if let snippet = itemDict["snippet"] as? Dictionary<NSObject,AnyObject> { 
          if let getTitle = snippet["title"] as? String{ title = getTitle} 
          if let getVideoId = snippet["resourceId"] as? Dictionary<NSObject,AnyObject> { 
           if let getVideoId2 = getVideoId["videoId"] as? String{ videoId = getVideoId2} 
          } 
          if let myThumb = snippet["thumbnails"] as? Dictionary<NSObject,AnyObject>{ 
           if let myHighThumb = myThumb["high"] as? Dictionary<NSObject,AnyObject>{ 
            if let thumbUrl = myHighThumb["url"] as? String{ 
             thumbnail = thumbUrl 
            } 
           } 
          } 
         } 
        } 
        if (thumbnail != "" && videoId != ""){ 
         self.arrayYoutubeTrailers.append(["title": title,"videoId": videoId, "thumbnail": thumbnail]) 
        } 
       } 
       self.delegate?.youtubeTrailersDidUpdate(self.arrayYoutubeTrailers) 
      } else { 
       self.delegate?.youtubeTrailersFailed() 
      } 
     } 
    } 

因爲答案可以例如鴕鳥政策有「縮略圖」或「標題」,我已經得到了這樣的一些答案,我要檢查一切或應用程序將崩潰。

只是爲了讓你知道,這是在回答

{ 
    contentDetails =  { 
     videoId = "va-0o_xBVnU"; 
    }; 
    etag = "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/spb02vX6UoYkYSIcBFTCzlPQ3Es\""; 
    id = "PLegkaUUrMBQZ7KYS4cdBhF5aRmyOFe-zLBqlqoAM_wH4"; 
    kind = "youtube#playlistItem"; 
    snippet =  { 
     channelId = UCi8e0iOVk1fEOogdfu4YgfA; 
     channelTitle = "Movieclips Trailers"; 
     description = "Subscribe to TRAILERS: http://bit.ly/sxaw6h\nSubscribe to COMING SOON: http://bit.ly/H2vZUn\nLike us on FACEBOOK: http://goo.gl/dHs73\nFollow us on TWITTER: http://bit.ly/1ghOWmt \nMoney Monster Official Trailer #1 (2016) - George Clooney, Julia Roberts Movie HD\n\nIn the taut and tense thriller Money Monster, Lee Gates (George Clooney) is a bombastic TV personality whose popular financial network show has made him the money wiz of Wall Street. But after he hawks a high tech stock that mysteriously crashes, an irate investor (Jack O'Connell) takes Gates, his crew, and his ace producer Patty Fenn (Julia Roberts) hostage live on air. Unfolding in real time, Gates and Fenn must find a way to keep themselves alive while simultaneously uncovering the truth behind a tangle of big money lies.\n\n\nThe Fandango MOVIECLIPS Trailers channel is your destination for the hottest new trailers the second they drop. Whether it's the latest studio release, an indie horror flick, an evocative documentary, or that new RomCom you've been waiting for, the Fandango MOVIECLIPS team is here day and night to make sure all the best new movie trailers are here for you the moment they're released.\n\nIn addition to being the #1 Movie Trailers Channel on YouTube, we deliver amazing and engaging original videos each week. Watch our exclusive Ultimate Trailers, Showdowns, Instant Trailer Reviews, Monthly MashUps, Movie News, and so much more to keep you in the know.\n\nHere at Fandango MOVIECLIPS, we love movies as much as you!"; 
     playlistId = "PLScC8g4bqD47c-qHlsfhGH3j6Bg7jzFy-"; 
     position = 26; 
     publishedAt = "2016-01-13T17:25:53.000Z"; 
     resourceId =   { 
      kind = "youtube#video"; 
      videoId = "va-0o_xBVnU"; 
     }; 
     thumbnails =   { 
      default =    { 
       height = 90; 
       url = "https://i.ytimg.com/vi/va-0o_xBVnU/default.jpg"; 
       width = 120; 
      }; 
      high =    { 
       height = 360; 
       url = "https://i.ytimg.com/vi/va-0o_xBVnU/hqdefault.jpg"; 
       width = 480; 
      }; 
      maxres =    { 
       height = 720; 
       url = "https://i.ytimg.com/vi/va-0o_xBVnU/maxresdefault.jpg"; 
       width = 1280; 
      }; 
      medium =    { 
       height = 180; 
       url = "https://i.ytimg.com/vi/va-0o_xBVnU/mqdefault.jpg"; 
       width = 320; 
      }; 
      standard =    { 
       height = 480; 
       url = "https://i.ytimg.com/vi/va-0o_xBVnU/sddefault.jpg"; 
       width = 640; 
      }; 
     }; 
     title = "Money Monster Official Trailer #1 (2016) - George Clooney, Julia Roberts Movie HD"; 
    }; 
} 

預先感謝一個項目。

+0

邁克爾斯回答與SwiftyJSON會做奇蹟。 –

回答

1

您可以將「if let」代碼鏈接在一起,因此至少它看起來不那麼難看。然後,而不是有;

if let ... 
    if let ... 
     if let ... etc 

你必須

if let ..., 
    let ..., 
    let ... { 
    // All good, do stuff 
} else { 
    // Something went wrong 
} 

然後你只需要處理與其他人的條件。

+0

謝謝@邁克爾,這肯定會有所幫助。 – ClaytonAV