【发布时间】:2017-10-14 12:18:32
【问题描述】:
我正在使用 YouTube API 从频道获取视频,我想获取标题:
let url = URL(string: "https://www.googleapis.com/youtube/v3/search?key=**********&channelId=UCFNHx0ppCqm4EgPzEcOc29Q&part=snippet,id&order=date&maxResults=50")
let task = URLSession.shared.dataTask(with: url!) { (data, reponse, error) in
if error != nil {
print("ERROR")
}else{
if let content = data {
do{
if let myJsonArray = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String:Any] {
if let itemsJson = myJsonArray["items"] as? [[String:Any]] {
//var i:Int = 0
for i in 0 ..< itemsJson.count {
print("-----------------------------")
let snippetDict = itemsJson[i]["snippet"] as! Dictionary<NSObject, AnyObject>
print(snippetDict["title"] as String) //NOT WORKING
}
}
}
}catch{
print("ERROR 2")
}
}
}
}
task.resume()
但是当我将所有标题打印为print(snippetDict["title"] as String) 时,Xcode 会说:
对成员“下标”的不明确引用。
如何获取视频的标题?
谢谢!
【问题讨论】:
标签: ios json swift youtube-api