title: How to display a playlist of a YouTube channel using YouTube API in Swift3
body: <p>Question 1 :</p>
  
  <p>I am using YouTube API to display a playlist of videos in a <code>UITableView</code> but it's not working.  It's working fine when I make it for a single video, one video appears in the <code>UITableView</code>.</p>
  
  <p>How can I display a playlist of any YouTube channel? I am using this code in my <code>UITableView</code>.</p>
  
  <p>My <code>UITableView</code> code :</p>
  
  <pre><code> import UIKit
   import AVFoundation
  
   class YTViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, AVAudioPlayerDelegate {
  
  @IBOutlet weak var txtSearch: UITextField!
  @IBOutlet weak var searchResultTableView: UITableView!
  
  // Set up a network session
  let session = URLSession.shared
  
  // ReST GET static String parts
  let BASE_URL: String = "https://www.googleapis.com/youtube/v3/"
  let SEARCH_VIDEO: String = "channels?part=snippet&amp;q="
  let VIDEO_TYPE: String = "&amp;id=UCJIc9yX_3iHE2CfmUqoeJKQ&amp;key="
  let API_KEY: String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  
  @IBAction func btnSearchClicked(_ sender: UIButton) {
  
  }
  
  func getVideoList() {
  
      let methodArguments: [String: AnyObject] = [
          "query": txtSearch.text! as AnyObject
      ]
  
      // Format the search string (video title) for http request
      let videoTitle: String = escapedParameters(methodArguments)
  
      // Make the query url
      // sample: https://www.googleapis.com/youtube/v3/search?part=snippet&amp;q=werewolf&amp;type=video&amp;key=AIzaSyDDqTGpVR7jxeozoOEjH6SLaRdw0YY-HPQ
  
      let searchVideoByTitle = BASE_URL + SEARCH_VIDEO + videoTitle + VIDEO_TYPE + API_KEY
  
      print("#####################\(searchVideoByTitle)")
      if let url = URL(string: searchVideoByTitle) {
          let request = URLRequest(url: url)
          // Initialise the task for getting the data
          initialiseTaskForGettingData(request, element: "items")
      }
  }
  
  // Array to store all the desired values dictionaries
  var videosArray: Array&lt;Dictionary&lt;String, AnyObject&gt;&gt; = [[String: AnyObject]]()
  
  func initialiseTaskForGettingData(_ request: URLRequest, element: String) {
  
      // Initialize task for getting data
      // Refer to http://www.appcoda.com/youtube-api-ios-tutorial/
  
      let task = session.dataTask(with: request, completionHandler: {(data, HTTPStatusCode, error) in
  
  
          // Handler in the case of an error
          if error != nil {
              print(error as Any)
              return
          }
          else {
              // Parse that data received from the service
              let resultDict: [String: AnyObject]!
              do {
                  // Convert the JSON data to a dictionary
                  resultDict = try JSONSerialization.jsonObject(with: data! as Data, options: .allowFragments) as! [String: AnyObject]
  
                   print("***************************\(resultDict)")
  
                  // Get the first item from the returned items
                  if let itemsArray = (resultDict as AnyObject).value(forKey: element) as? NSArray {
  
                      // Remove all existing video data
                      self.videosArray.removeAll()
  
                      for index in 0..&lt;itemsArray.count {
  
                          // Append the desiredVaules dictionary to the videos array
                          self.videosArray.append(self.unwrapYoutubeJson(arrayToBeUnwrapped: itemsArray, index: index))
  
                      }
  
                      // Asynchronously reload the data and display on the tableview
                      DispatchQueue.main.async {
                          // Reload the tableview
                          self.searchResultTableView.reloadData()
                      }
                  }
  
              } catch let jsonError {
                  print(jsonError)
              }
          }
      })
      // Execute the task
      task.resume()
  }
  
  func unwrapYoutubeJson(arrayToBeUnwrapped: NSArray, index: Int) -&gt; [String: AnyObject]{
  
      let firstItemDict = arrayToBeUnwrapped[index] as! [String: AnyObject]
  
      // Get the snippet dictionary that contains the desired data
      let snippetDict = firstItemDict["snippet"] as! [String: AnyObject]
  
      // Dictionary to store desired video contents for display on tableview
      // desired values - "Title", "Description", "Thumbnail"
      var desiredValuesDict = [String: AnyObject]()
  
      desiredValuesDict["title"] = snippetDict["title"]
      desiredValuesDict["description"] = snippetDict["description"]
  
      // Further unwrap to get the Thumbnail default URL
      let thumbnailDict: [String: AnyObject]
      thumbnailDict = snippetDict["thumbnails"] as! [String: AnyObject]
      let defaultThumbnailDict = thumbnailDict["default"] as! [String: AnyObject]
  
      desiredValuesDict["thumbnail"] = defaultThumbnailDict["url"]
  
      //Get the id dictionary that contains videoId
      let idDict = firstItemDict["id"] as? [String: AnyObject]
      desiredValuesDict["videoId"] = idDict?["videoId"] 
  
      return desiredValuesDict
  }
  
  // Helper function: Given a dictionary of parameters, convert to a string for a url
  func escapedParameters(_ parameters: [String : AnyObject]) -&gt; String {
  
      var urlVars = [String]()
  
      for (key, value) in parameters {
  
          // Make sure that it is a string value
          let stringValue = "\(value)"
  
          // Escape it
          let escapedValue = stringValue.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
  
          //Append it
          urlVars += [key + "=" + "\(escapedValue!)"]
      }
  
      return (!urlVars.isEmpty ? "" : "") + urlVars.joined(separator: "&amp;")
  }
  
  // MARK: UITableView method implementation
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SearchResultTableViewCell
  
      let videoSelected = videosArray[indexPath.row]
      cell.updateIU(video: videoSelected)
  
       cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  
      let id = videosArray[indexPath.row]["videoId"] as? String
  
      print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\(id)")
       return cell
   }
  
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return videosArray.count
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let destination = segue.destination as? playerViewController {
          if let selectedRowIndexPath = searchResultTableView.indexPathForSelectedRow?.row {
              destination.mytitle = videosArray[selectedRowIndexPath]["title"] as! String
              destination.mydescript = videosArray[selectedRowIndexPath]["description"] as! String
              destination.myvideoId = videosArray[selectedRowIndexPath] ["videoId"] as? String
          }
      }
    }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
       getVideoList()
  
      searchResultTableView.dataSource = self
      searchResultTableView.delegate = self
  }
  
  
  }
  </code></pre>
  
  <p>Question 2 :</p>
  
  <p>When I am trying to play a video using <code>YTPlayerHelper</code> it's not working:
  <code>fatal error: unexpectedly found nil while unwrapping an Optional value</code> and the video ID appears as nil.</p>
  
  <p>How can I play the video using the <code>YTPlayerHelper</code>? This is how I am playing the video:</p>
  
  <pre><code>import UIKit
  import youtube_ios_player_helper
  
  class playerViewController: UIViewController {
  
  
  @IBOutlet weak var MyPlayer: YTPlayerView!
  @IBOutlet weak var txtTitle: UITextView!
  @IBOutlet weak var txtDescript: UITextView!
  
  var mytitle: String!
  var mydescript: String!
  var myvideoId : String!
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\(myvideoId)")
  
      MyPlayer.load(withVideoId: myvideoId!)
  
      txtTitle.text = mytitle
      txtDescript.text = mydescript
  
  }
  }
  </code></pre>