我已经实现了下面的代码,它们工作正常,它显示正确的数据,并在拉动后刷新。但是,当将垃圾邮件快速发送几次时,最终会导致致命错误-索引超出范围。

这让我发疯,有人可以看到我的代码有什么问题吗?

    override func viewDidLoad()
{
    super.viewDidLoad()

    nameArray.removeAll(keepingCapacity: false)
    addressArray.removeAll(keepingCapacity: false)


    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Reloading")
    refresher.addTarget(self, action: #selector(downloadJsonWithURL), for: UIControlEvents.valueChanged)
    tableView.addSubview(refresher)
    self.downloadJsonWithURL()

}


func downloadJsonWithURL()
{
    // Emtpy arrays to avoid index out of range crash
    nameArray.removeAll(keepingCapacity: false)
    addressArray.removeAll(keepingCapacity: false)

    let url = NSURL(string: urlString)

    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in


        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {

            if let projectArray = jsonObj!.value(forKey: "project") as? NSArray {
                for project in projectArray{
                    if let projectDict = project as? NSDictionary {
                        if let name = projectDict.value(forKey: "societe") {
                            self.nameArray.append(name as! String)
                        }
                    }
                    if let projectDict = project as? NSDictionary {
                        if let name = projectDict.value(forKey: "address") {
                            self.addressArray.append(name as! String)
                        }
                    }
                }
            }

            OperationQueue.main.addOperation(
                {
                    self.tableView.reloadData()
                    self.refresher.endRefreshing()
            })
        }
    }).resume()

}

// Count length of array and create number of cells accordingly
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return nameArray.count
}

// Create cell data from arrays
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    cell.projectDesc.text = nameArray[indexPath.row].uppercased()
    cell.projectAddress.text = addressArray[indexPath.row]
    return cell
}

最佳答案

问题是您要在接收响应之前从阵列中删除对象。它创建了一个场景,其中numberOfRowsInSection返回不同​​的值,并且在调用cellForRowAtIndexPath时,它在数组中具有不同的(零)项,由于索引超出范围问题,导致应用程序崩溃。这通常是多线程问题。

为解决此问题,仅当成功从API调用接收数据时,才清空数组。为此,您需要在for project in projectArray{上方添加removeAll元素的代码。

,使您的代码类似于:

override func viewDidLoad()
{
    super.viewDidLoad()

    nameArray.removeAll(keepingCapacity: false)
    addressArray.removeAll(keepingCapacity: false)

    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Reloading")
    refresher.addTarget(self, action: #selector(downloadJsonWithURL), for: UIControlEvents.valueChanged)
    tableView.addSubview(refresher)
    self.downloadJsonWithURL()
}

func downloadJsonWithURL()
{
    let url = NSURL(string: urlString)

    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in


        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {

            if let projectArray = jsonObj!.value(forKey: "project") as? NSArray {
                nameArray.removeAll(keepingCapacity: false)
                addressArray.removeAll(keepingCapacity: false)

                for project in projectArray{
                    if let projectDict = project as? NSDictionary {
                        if let name = projectDict.value(forKey: "societe") {
                            self.nameArray.append(name as! String)
                        }
                    }
                    if let projectDict = project as? NSDictionary {
                        if let name = projectDict.value(forKey: "address") {
                            self.addressArray.append(name as! String)
                        }
                    }
                }
            }

            OperationQueue.main.addOperation(
                {
                    self.tableView.reloadData()
                    self.refresher.endRefreshing()
            })
        }
    }).resume()
}

10-08 19:18