私はjsonの解析でYouTubeチャンネルと動画を取得しようとしていますが、私はSwift 3を初めて使っています。消えて、結果は表示されず、ログにエラーはありません。Swift 3の動画を解析しても結果が表示されず、ロードされ続ける - IOS 10
これは私のコードである:getVideosForChannelAtIndex FUNC
1 /:
func getVideosForChannelAtIndex(_ index: Int!) {
let playlistID = channelsDataArray[index]["playlistID"] as! String
let urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=\(playlistID)&key=\(apiKey)"
let targetURL = URL(string: urlString)
var url = URLRequest(url: targetURL!)
url.httpMethod = "GET"
URLSession.shared.dataTask(with: url) { (data, response, error) in
let httpStatus = response as? HTTPURLResponse
if httpStatus!.statusCode == 200 && error != nil {
do {
let resultsDict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! Dictionary<String, AnyObject>
let items: Array<Dictionary<String, AnyObject>> = resultsDict["items"] as! Array<Dictionary<String, AnyObject>>
for i in 0 ..< items.count {
let playlistSnippetDict = (items[i] as Dictionary<String, AnyObject>)["snippet"] as! Dictionary<String, AnyObject>
var desiredPlaylistItemDataDict = Dictionary<String, AnyObject>()
desiredPlaylistItemDataDict["title"] = playlistSnippetDict["title"]
desiredPlaylistItemDataDict["thumbnail"] = ((playlistSnippetDict["thumbnails"] as! Dictionary<String, AnyObject>)["default"] as! Dictionary<String, AnyObject>)["url"]
desiredPlaylistItemDataDict["videoID"] = (playlistSnippetDict["resourceId"] as! Dictionary<String, AnyObject>)["videoId"]
self.videosArray.append(desiredPlaylistItemDataDict)
self.tableView.reloadData()
}
} catch {
print(error)
}
}
else {
print("Error while loading channel videos: \(error)")
}
self.activityView.isHidden = true
}
}
2/FUNC getChannelDetails:
func getChannelDetails(_ useChannelIDParam: Bool) {
var urlString: String!
if !useChannelIDParam {
urlString = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails,snippet&forUsername=\(desiredChannelsArray[channelIndex])&key=\(apiKey)"
}
else {
urlString = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails,snippet&id=\(desiredChannelsArray[channelIndex])&key=\(apiKey)"
}
let targetURL = URL(string: urlString)
var request = URLRequest(url: targetURL!)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { (data, response, error) in
let httpStatus = response as? HTTPURLResponse
if httpStatus!.statusCode == 200 && error != nil {
do {
let resultsDict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! Dictionary<String, AnyObject>
let items: AnyObject! = resultsDict["items"] as AnyObject!
let firstItemDict = (items as! Array<AnyObject>)[0] as! Dictionary<String, AnyObject>
let snippetDict = firstItemDict["snippet"] as! Dictionary<String, AnyObject>
var desiredValuesDict: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
desiredValuesDict["title"] = snippetDict["title"]
desiredValuesDict["description"] = snippetDict["description"]
desiredValuesDict["thumbnail"] = ((snippetDict["thumbnails"] as! Dictionary<String, AnyObject>)["default"] as! Dictionary<String, AnyObject>)["url"]
desiredValuesDict["playlistID"] = ((firstItemDict["contentDetails"] as! Dictionary<String, AnyObject>)["relatedPlaylists"] as! Dictionary<String, AnyObject>)["uploads"]
self.channelsDataArray.append(desiredValuesDict)
self.tableView.reloadData()
self.channelIndex += 1
if self.channelIndex < self.desiredChannelsArray.count {
self.getChannelDetails(useChannelIDParam)
}
else {
self.activityView.isHidden = true
}
} catch {
print(error)
}
} else {
print("Error while loading channel details: \(error)")
}
}
}
}
3/FUNC textFieldShouldReturn:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
activityView.isHidden = false
var type = "channel"
if segmentDisplay.selectedSegmentIndex == 1 {
type = "video"
videosArray.removeAll(keepingCapacity: false)
}
var urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=\(textField.text)&type=\(type)&key=\(apiKey)"
urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let targetURL = URL(string: urlString)
var request = URLRequest(url: targetURL!)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { (data, response, error) in
let httpStatus = response as? HTTPURLResponse
if httpStatus!.statusCode == 200 && error != nil {
do {
let resultsDict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! Dictionary<String, AnyObject>
let items: Array<Dictionary<String, AnyObject>> = resultsDict["items"] as! Array<Dictionary<String, AnyObject>>
for i in 0 ..< items.count {
let snippetDict = items[i]["snippet"] as! Dictionary<String, AnyObject>
if self.segmentDisplay.selectedSegmentIndex == 0 {
self.desiredChannelsArray.append(snippetDict["channelId"] as! String)
}
else {
var videoDetailsDict = Dictionary<String, AnyObject>()
videoDetailsDict["title"] = snippetDict["title"]
videoDetailsDict["thumbnail"] = ((snippetDict["thumbnails"] as! Dictionary<String, AnyObject>)["default"] as! Dictionary<String, AnyObject>)["url"]
videoDetailsDict["videoID"] = (items[i]["id"] as! Dictionary<String, AnyObject>)["videoId"]
self.videosArray.append(videoDetailsDict)
self.tableView.reloadData()
}
}
} catch {
print(error)
}
if self.segmentDisplay.selectedSegmentIndex == 0 {
self.getChannelDetails(true)
}
}
else {
print("Error while loading channel videos: \(error)")
}
self.activityView.isHidden = true
}
return true
}
ここではresume()を呼び出しますか? .resume()をdataTaskの中かっこに追加する必要があります –
これはコードが多いため、投稿する前にインデントを修正する必要があります。 –
他のメソッドを追加してコードを更新しましたが、指定した.resume()を挿入する場所が見つかりません。コードや質問が気になるようであれば私の謝罪ですが、私はIOSとStackoverflowの両方に新しいです。ありがとうレオ;)。 –