Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用可解码到TableView节(数组中包含数组)解析JSON_Ios_Json_Swift_Uitableview_Decodable - Fatal编程技术网

Ios 使用可解码到TableView节(数组中包含数组)解析JSON

Ios 使用可解码到TableView节(数组中包含数组)解析JSON,ios,json,swift,uitableview,decodable,Ios,Json,Swift,Uitableview,Decodable,我正在尝试将api中的数据解析为包含节的tableView。最终结果将是一个与一个月相对应的部分标题,以及一行与该月发布的视频列表相对应的内容。视频可能不包括海报或描述 我尝试实现for-in-loop函数来在检索数据后更新模型,这在我开始尝试实现部分之前效果很好。我可以将json响应打印到控制台并接收完整响应 以下是原始JSON结构的示例: { "page": { "type": "videos", "sections": [{

我正在尝试将api中的数据解析为包含节的tableView。最终结果将是一个与一个月相对应的部分标题,以及一行与该月发布的视频列表相对应的内容。视频可能不包括海报或描述

我尝试实现for-in-loop函数来在检索数据后更新模型,这在我开始尝试实现部分之前效果很好。我可以将json响应打印到控制台并接收完整响应

以下是原始JSON结构的示例:

{
    "page": {
        "type": "videos",
        "sections": [{
            "title": "September",
            "videos": [{
                "title": "Some Video",
                "description": "Video Description",
                "poster": "",
                "url": "url"
            }]
        }, {
            "title": "August 2019",
            "videos": [{
                "title": "Some Video",
                "description": "",
                "poster": "Some Image",
                "url": "url"
            }, {
                "title": "Some Video",
                "description": "No Description",
                "poster"",
                "url": "url"
            }]
        }]
    }
}
这是我的模型:

struct Root:可解码{
让第页:第页
}
结构页:可解码{
let类型:String
let Section:[视频部分]
}
结构视频部分:可解码{
标题:字符串
播放视频:[视频]
}
结构视频:可解码{
让视频标题:字符串
让我们描述一下:字符串?
让海报:绳子?
让url:String
枚举编码键:字符串,编码键{
案例videoTitle=“标题”
案例videoDescription=“描述”
case poster=“poster”
case url=“url”
}
}
以下是may网络呼叫解析:

func-getVideoData(url:String){
guard let videoUrl=URL(字符串:“Video_URL”)else{return}
URLSession.shared.dataTask(带:videoUrl){(数据、响应、错误)在
guard let data=data else{return}
做{
let decoder=JSONDecoder()
let responseData=尝试decoder.decode(Root.self,from:data)
DispatchQueue.main.async{
self.videoTableView.reloadData()
}
}捉迷藏{
打印(“错误”,错误)
}
}1.简历()
}
这是我的桌面视图:

var allvideosArray=[Video]()
var allSectionsArray=[VideoSection]()
var rootArray:Root?
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“customVideoCell”,for:indexPath)作为!customVideoCell
cell.videoDescriptionPlaceholder.text=Video.CodingKeys.videoDescription.rawValue
cell.videoTitlePlaceholder.text=Video.CodingKeys.videoTitle.rawValue
返回单元
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String?{
返回allSectionsArray[section]。标题
}
func numberOfSections(在tableView:UITableView中)->Int{
返回allSectionsArray.count
}    
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回所有VideoSarray.count
}
当我尝试
打印(responseData.page.sections.videos)
时,我收到错误“type'[VideoSection]'的值没有成员“videos”,这使我相信问题与
[sections]
数组内部的
[videos]
数组有关。

您可以尝试

var page:Page?



sections
是一个数组,您不能执行
page.sections.videos
您可以执行
page.sections.first?.videos
page.sections[someIndex]。视频
let responseData = try decoder.decode(Root.self, from: data)
self.page = responseData.page 
DispatchQueue.main.async { 
    self.videoTableView.reloadData()
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "customVideoCell", for: indexPath) as! CustomVideoCell
    let item = page!.sections[indexPath.section].videos[indexPath.row]
    cell.videoDescriptionPlaceholder.text = item.videoDescription 
    cell.videoTitlePlaceholder.text = item.videoTitle
    return cell
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return page?.sections[section].title
} 

func numberOfSections(in tableView: UITableView) -> Int {
    return page?.sections.count ?? 0
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return page?.sections[section].videos.count ?? 0

}