致命错误。数组索引超出索引路径范围 - swift

时间:2023-01-15 16:41:41

My following code is part of a search controller. It works without a problem but after 8-10 searches, I encounter fatal error (Thread 1 : EXC_BAD_INSTRUCTION) on following line :

我的以下代码是搜索控制器的一部分。它运行没有问题,但在8-10次搜索后,我在下面的行遇到致命错误(线程1:EXC_BAD_INSTRUCTION):

let movie = filteredMovies[indexPath.item]

Could you please advise the way of solving this kind of problems.

你能否告诉我解决这类问题的方法。

extension searchResults: UITableViewDataSource, UITableViewDelegate {

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("searchCell", forIndexPath: indexPath) as! searchResultCell

    let movie = filteredMovies[indexPath.item]
    cell.searchLabel.text = movie.title

    let fileUrl = NSURL(string: movie.thumb)!

    if let data = NSData(contentsOfURL: fileUrl)
    {
        cell.searchImage.contentMode = UIViewContentMode.ScaleAspectFit
        cell.searchImage.image = UIImage(data: data)


        //  to make images rounded
        cell.searchImage.backgroundColor = UIColor.clearColor()
        cell.searchImage.layer.cornerRadius = 15.0
        cell.searchImage.clipsToBounds = true

        cell.backgroundColor = UIColor.clearColor()

    }

    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

{

  return filteredMovies.count

}

}

1 个解决方案

#1


0  

You can check if indexPath.item is within the bounds of the array filteredMovies:

您可以检查indexPath.item是否在数组filteredMovies的范围内:

if (indexPath.item >= 0 || indexPath.item < movie.count) {

    // now you can safely use filteredMovies[indexPath.item]

} else {
    // print something so you can investigate
}

And if you wonder why indexPath.item might be out of the bounds of filteredMovies, it's other programming logic you have to investigate (maybe you remove some elements in filteredMovies after the tableView is loaded).

如果您想知道为什么indexPath.item可能超出filteredMovies的范围,那么您需要调查其他编程逻辑(可能在加载tableView之后删除filteredMovies中的一些元素)。

After all, always checking the bounds is a good thing when you have doubt or want to make sure it will never cause crash (fault tolerance concept).

毕竟,当您怀疑或想要确保它永远不会导致崩溃(容错概念)时,总是检查边界是一件好事。

#1


0  

You can check if indexPath.item is within the bounds of the array filteredMovies:

您可以检查indexPath.item是否在数组filteredMovies的范围内:

if (indexPath.item >= 0 || indexPath.item < movie.count) {

    // now you can safely use filteredMovies[indexPath.item]

} else {
    // print something so you can investigate
}

And if you wonder why indexPath.item might be out of the bounds of filteredMovies, it's other programming logic you have to investigate (maybe you remove some elements in filteredMovies after the tableView is loaded).

如果您想知道为什么indexPath.item可能超出filteredMovies的范围,那么您需要调查其他编程逻辑(可能在加载tableView之后删除filteredMovies中的一些元素)。

After all, always checking the bounds is a good thing when you have doubt or want to make sure it will never cause crash (fault tolerance concept).

毕竟,当您怀疑或想要确保它永远不会导致崩溃(容错概念)时,总是检查边界是一件好事。