1

I have looked through the similar topics in stackoverflow but none of those solutions seem to work for me.

I have an app that fetch video through youtube API . The following code is giving the error.

var arrayOfVideos = [Video]()

// I am getting the error for the following line and it says "Ambiguous use of 'subscript'.

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


    arrayOfVideos.append(videoObj)

}

I have also a class called Video where I define the Id, Title and etc... The above code refers here.

class Video: NSObject {

    var videoId:String = ""
    var videoTitle:String = ""
    var videoDescription:String = ""
    var videoThumbnailUrl:String = ""

}

I have tried to solve it by checking the link/suggestions below but it didn't work for me. I cannot seem to find the missing part.

Ambiguous Use of Subscript in Swift

Community
  • 1
  • 1
  • 1
    What is the `JSON` object? Show us how you created that. – Rob Apr 20 '16 at 21:48
  • Can you show the definition of the `JSON` variable please? – George Green Apr 20 '16 at 21:49
  • Thanks Rob and George for your attention on this case. I have the following lines for your reference. I couldn't post the full code as a comment. I hope this helps. class VideoModal: NSObject { var videoArray = [Video]() if let JSON = response.result.value { print("JSON: \(JSON)") var arrayOfVideos = [Video]() – Burak Pusat Apr 21 '16 at 17:24

1 Answers1

2

The problem here is almost certainly that your JSON is defined as a type that could be an array or a dictionary. With Xcode 7.1 ambiguity enforcement was levelled up, so you need to explicitly cast it to something that can be subscripted by a string. This should sort you nicely:

for video in (JSON as! NSDictionary)["items"] as! NSArray
Alex Curylo
  • 4,744
  • 1
  • 27
  • 37