I previously had problem with the JSON data but found a solution to it as suggested by alexcurylo in the link down below;
Ambiguous use of 'subscript' with NSArray & JSON
It was working fine for a while but now it is returning fatal error: unexpectedly found nil while unwrapping an Optional value on the following line: for video in (JSON as! NSDictionary)["items"] as! NSArray {. Any suggestion on this? I thought that might be related to thumbnails not having maxres but changing it default or high didn't work either.
let API_KEY = "BrowserKey"
let UPLOADS_PLAYLIST_ID = "playlistID"
var videoArray = [Video]()
var delegate: VideoModelDelegate?
func getFeedVideos() {
//Fetch the videos dynamically through the Youtube DATA API
Alamofire.request(.GET, "https://www.googleapis.com/youtube/v3/playlists", parameters: ["part":"snippet", "playlistId":UPLOADS_PLAYLIST_ID,"key":API_KEY], encoding: ParameterEncoding.URL, headers: nil).responseJSON { (response) -> Void in
if let JSON = response.result.value {
var arrayOfVideos = [Video]()
for video in (JSON as! NSDictionary)["items"] as! NSArray {
// create video objects off of the JSON response
let videoObj = Video()
videoObj.videoId = video.valueForKeyPath("snippet.resourceId.videoId") as! String
videoObj.videoTitle = video.valueForKeyPath("snippet.title") as! String
videoObj.videoDescription = video.valueForKeyPath("snippet.description") as! String
videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.default.url") as! String
arrayOfVideos.append(videoObj)
}
//when all the video objects constructed, 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()
}
}
}