Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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/0/amazon-s3/2.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
Swift 致命错误:索引超出范围UITableView indexPath.row_Swift_Uitableview - Fatal编程技术网

Swift 致命错误:索引超出范围UITableView indexPath.row

Swift 致命错误:索引超出范围UITableView indexPath.row,swift,uitableview,Swift,Uitableview,尝试从indexPAth.row的url加载图像时,出现索引超出范围错误 我的结构模型正确,可以从Json响应中获取URL。我可以在调试器窗口中获取URL,但只要我尝试在cellForRowAt:中获取图像,就会出现Fata错误 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return redditData.count } overri

尝试从indexPAth.row的url加载图像时,出现索引超出范围错误

我的结构模型正确,可以从Json响应中获取URL。我可以在调试器窗口中获取URL,但只要我尝试在cellForRowAt:中获取图像,就会出现Fata错误

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return redditData.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "RedditTableCell", for: indexPath) as! RedditTableCell

        let post = redditData[indexPath.row]

        // uncomment this print statement and I get the fata error
        //print(post.data.preview?.images?[indexPath.row].source.url)   

        // uncomment this and in the debugger it will show the images URL path
        //print(post.data.preview?[0].source.url)  

        // each cell has only one   mainImage 
        print(post.data.preview?.images?[0].source?.url)  // prints the image url path, for each cell.
        cell.thumbnail.sd_setImage(with: URL(string: post.data.thumbnail!))
        cell.mainImage.sd_setImage(with: URL(string: (post.data.preview?.images?.source!.url)!)) 
        // images is an array [images]

        return cell
    }
以下是调试器输出:

https://www.reddit.com/r/all/.json
Optional("https://external-preview.redd.it/aF3ZkA26B3LsRG6ugjJTHAgn5uL_3y_iniPkFMGTZqY.jpg?auto=webp&s=1554c0af19a4e1776a29878c4c0c1cb1c1324c15")
Optional("https://preview.redd.it/cy8jp6qvvb631.jpg?auto=webp&s=cc5a0c15d130a4c54f33963ccce4cd1588871e85")
Optional("https://preview.redd.it/b39b3dgalb631.jpg?auto=webp&s=19b137dc245a243acda7c6a08b7d7e3d5b2acda6")
2019-06-24 13:37:46.137915-0500 MyRedditBrowser[15273:1933069] Task <77A8D26A-6460-48A2-9140-381470ABB7DF>.<1> finished with error - code: -1002
2019-06-24 13:37:46.140059-0500 MyRedditBrowser[15273:1933066] Task <77A8D26A-6460-48A2-9140-381470ABB7DF>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=default, NSErrorFailingURLKey=default, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <77A8D26A-6460-48A2-9140-381470ABB7DF>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <77A8D26A-6460-48A2-9140-381470ABB7DF>.<1>, NSUnderlyingError=0x6000016d0600 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}} [-1002]
那么我错过了什么超基本的东西呢?有时我根本看不到任何错误???

你是说

print(post.data.preview?.images?[indexPath.row].source.url)
如果图像为空或其计数小于indexath.row+1,则会崩溃。所以,事先设定一个条件来检查

if let count = post.data.preview?.images?.count, count > indexPath {

post已经是该行的数据。您也不希望使用indexPath获取行的post中的图像。我是否将图像放在括号中?[图片]像那样?但我也会这样出错。post.data.preview?.images?.source!。网址。出现以下错误:“[Image]”类型的值没有成员“source”,但source是一个成员。您说的是post.data.preview?.images?[0]。如果图像是空数组,您将崩溃。您还使用了感叹号。这也是一种以另一种方式崩溃的邀请。哦,然后在括号中,我会将我的cell.mainImage=image放在那个位置!这不太可能是正确的解决方案。它将修复崩溃,但并不能真正解决根本问题。post已经是行特定的数据。您根本不应该使用indexPath来访问post中的图像。@rmaddy我相信您是对的;我只是看着撞车线,试图解释撞车的原因。请随意写一个更好的答案。