使用NSArray& amp;'模糊地使用'下标' JSON [英] Ambiguous use of 'subscript' with NSArray & JSON
问题描述
我已经查看了stackoverflow中的类似主题,但这些解决方案似乎都不适合我。
I have looked through the similar topics in stackoverflow but none of those solutions seem to work for me.
我有一个通过youtube API获取视频的应用。以下代码给出了错误。
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)
}
我还有一个名为Video的课程我在哪里定义Id,Title等...上面的代码在这里指的。
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.
推荐答案
这里的问题几乎肯定是你的 JSON
被定义为可以是数组或字典的类型。使用Xcode 7.1时,歧义强制执行得到了提升,因此您需要将其明确地转换为可以通过字符串下标的内容。这应该很好地排序你:
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
这篇关于使用NSArray& amp;'模糊地使用'下标' JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!