简体   繁体   English

通过从阵列崩溃中删除项目并出现致命错误来删除tableView中的项目:索引超出范围

[英]Deleting item in tableView by removing item from array crashes with fatal error: Index out of range

I am trying to remove an item from a public static array that is been shown in a tableview. 我试图从显示在tableview中的公共静态数组中删除一个项目。 The deletion is occuring in a different viewcontroller and when I try to enter that view controller the app crashes with the error of 'Index out of range' 删除发生在其他ViewController中,当我尝试输入该View Controller时,应用崩溃,并显示“索引超出范围”错误

this is where i delete the Item 这是我删除项目的地方

func removeItem (info:Dictionary<String, Any>){
    let stringKey = info[KEYS.stringKey] as! String
    for var i in 0 ..< ItemsViewController.itemVideosList.count {

        let current = ItemsViewController.itemVideosList[i]
        let currentStringKey = current[KEYS.stringKey] as! String

        if stringKey == currentStringKey {

            ItemsViewController.itemVideosList.remove(at: i)
            return
        }
    }

I call this method on a button click here: 我在单击按钮的按钮上单击此方法:

@IBAction func FavoriteAction(_ sender: Any) {


    if Flag {
        removeFromFavorite(info: songInfo)
        Flag = false
        }
    else {
        ItemsViewController.itemVideosList.insert(songInfo, at: 0)
        favFlag = true
    }
}

I am populating the table view in 'ItemsViewController' viewDidLoad with this method 我正在使用此方法在'ItemsViewController'viewDidLoad中填充表格视图

func loadItems() {
    DispatchQueue.main.async {
        self.itemsTableView.reloadData()
    }
}

thanks 谢谢

You are trying to loop through when deleting. 您正在尝试在删除时进行遍历。 This leads to ItemsViewController.itemVideosList.count being invalid after your deletion. 删除后,这会导致ItemsViewController.itemVideosList.count无效。

  1. You can solve your problem simply by adding a break instead of a return 您只需添加一个中断而不是一个回报就可以解决您的问题

     if stringKey == currentStringKey { ItemsViewController.itemVideosList.remove(at: i) return } 
  2. More importantly, you should not modify a collection while iterating it. 更重要的是,您不应在迭代时修改集合。 This leads to undesired side effects. 这导致不希望的副作用。 It's better to just store the indexToDelete and then delete it outside of loop. 最好只存储indexToDelete,然后在循环外将其删除。

     func removeItem (info:Dictionary<String, Any>){ let indexToRemove = -1 let stringKey = info[KEYS.stringKey] as! String for var i in 0 ..< ItemsViewController.itemVideosList.count { let current = ItemsViewController.itemVideosList[i] let currentStringKey = current[KEYS.stringKey] as! String if stringKey == currentStringKey { indexToDelete = i break } } ItemsViewController.itemVideosList.remove(at: indexToDelete) } 

Note: 注意:

You could make your iteration cleaner using higher-order functions like so: 您可以使用以下高阶函数使迭代更整洁:

var list = ["a", "b", "c"]

let index = list.index { (str) -> Bool in
    if str == "b" {
        return true
    }
    return false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM