-1

Everything was working fine in Swift 2but after Swift 3 upgrade it is failing. It is simply giving the following error:

Type "Any" has no subscript members

on the following line:

for video in JSON["items"] as? NSArray {

My previous question and solution to it can be found here:

Ambiguous use of 'subscript' with NSArray & JSON

I have also looked through the suggested questions and answers while typing my question but couldn't come up with a solution so far.

class videoModel: NSObject {

let API_KEY = "Xxxxxxxxxxx"

let UPLOADS_PLAYLIST_ID = "yyyyyyyyyyyyyyy"


var videoArray = [Video]()

var delegate: VideoModelDelegate?

let urladdress = "https://www.googleapis.com/youtube/v3/playlistItems"


func getFeedVideos() {

    Alamofire.request((urladdress), method: .get, parameters: ["part":"snippet", "playlistId": UPLOADS_PLAYLIST_ID,"key": API_KEY, "maxResults": "50"], encoding: JSONEncoding.default).responseJSON(completionHandler: { (response) -> Void in

        if let JSON = response.result.value  {

            var arrayOfVideos = [Video]()

            print(JSON)

           for video in JSON["items"] as? NSArray {

                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.maxres.url") as! String

                arrayOfVideos.append(videoObj)

            }

            self.videoArray = arrayOfVideos

            if self.delegate != nil {

                self.delegate?.dataReady()

            }
        }
    })
}
}
Community
  • 1
  • 1
  • Hey, did you fix this, if so please share the final answer. I'm having the exact same error. I'm using the exact same code too. please share the final code after fixing this error. Thanks – user3707644 Oct 25 '16 at 03:43
  • 1
    Please have a look at this one: cl.ly/02030q1Q0E10. Let me know if you need further information. – Burak Pusat Nov 05 '16 at 06:25
  • Can u please share the whole source code for the class. I am still having problem with: if let JSON = response.result.value as? NSArray, it's always false – user3707644 Nov 17 '16 at 05:43
  • Please find the full source code in the link down below; https://dl.dropboxusercontent.com/u/20847754/videoModel.swift – Burak Pusat Nov 20 '16 at 06:25
  • Hey, can u share with me the whole project. I am really having a hard time fixing it. i tried everything to get to run but i am still having issue with project. please help me. Thanks – user3707644 Nov 22 '16 at 06:30
  • @user3707644 send me your project. I will have a look at it and show you the way to solve the problem -if I can-. – Burak Pusat Dec 03 '16 at 17:57

1 Answers1

1

You need to specify the type of your JSON object to [String : Any].

 if let JSON = response.result.value  as?  [String : Any] {
       if let items =  JSON["items"] as? [[String : Any]] {
            for video in items {
                  //Here use video["snippet.resourceId.videoId"] instead of value for key
            }
       }
 }

Note : In swift it is batter if you use swift generic Array and dictionary objects instead of NSArray & NSDictionary.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • When I do that, I get an error on the following line; for video in JSON["items"] as? NSArray and suggest me to change it to the following: for video in (JSON["items"] as? NSArray)! But then I get an error on the following line with the error message: Cannot invoke 'value' with an argument list of type '(String)'" videoObj.videoId = (video as AnyObject).value("snippet.resourceId.videoId") as! String – Burak Pusat Sep 28 '16 at 17:43
  • Check the edited answer – Nirav D Sep 28 '16 at 17:47