【问题标题】:Im populating a PickerView from Core data and getting the error Index Out Of Range Swift 5我从核心数据填充 PickerView 并得到错误 Index Out Of Range Swift 5
【发布时间】:2020-02-11 02:59:30
【问题描述】:

我对 swift 非常陌生,我试图了解使用 CoreData 填充 UIPickerView。在观看了有关使用 UITableView 执行此操作的视频后,我为 UIPickerView 实现了相同的代码。代码运行良好,直到我从 CoreData 中删除所有内容,然后它因错误索引超出范围而崩溃。

我知道它崩溃的代码行,但我知道如何修复它。我在这里阅读了许多关于索引超出范围的问题,但我看不到任何相同的内容。

这是代码,表格视图代码来自视频,我确实理解了 80%。

崩溃的代码行就在底部

import UIKit

import CoreData

// Global Variables and Constants

let appDelegate = UIApplication.shared.delegate as? AppDelegate

class ViewController: UIViewController {


    // Outlets
    @IBOutlet weak var tv: UITableView!

    @IBOutlet weak var pickerView: UIPickerView!

    // Varables
    var taskArray = [Task]()


    // Constants
    let cellid = "CellId"

    func randomNumber(num: Int) -> Int{
          return Int(arc4random_uniform(UInt32(num)))
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        callDelegates()
    }

    override func viewWillAppear(_ animated: Bool) {
        fetchyData()
        tv.reloadData()
        pickerView.reloadAllComponents()

    }

    func fetchyData(){
        fetchData { (done) in
            if done {
                if taskArray.count > 0 {
                    tv.isHidden = false
                    pickerView.isHidden = false
                } else {
                    tv.isHidden = true
                    pickerView.isHidden = true
                }
            }
        }
    }



    func callDelegates(){
        tv.delegate = self
        tv.dataSource = self
        tv.isHidden = true
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.isHidden = self
    }



    @IBAction func btnPressedButton(_ sender: Any) {
                  //textLbl.text = "\(items)"
        pickerView.selectRow(randomNumber(num: 1000), inComponent: 0, animated: true)

    }

}

extension ViewController: UITableViewDataSource, UITableViewDelegate {

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

        return taskArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! TableViewCell
        let task = taskArray[indexPath.row]
        cell.taskLbl.text = task.taskDescription
        if task.taskStatus == true {
            cell.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
            cell.taskLbl.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        }

        return cell

    }
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .none
    }
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
            self.deleteData(indexPath: indexPath)
            self.fetchyData()
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }

        let taskStatusAction = UITableViewRowAction(style: .normal, title: "Completed") { (action, indexPath) in
            self.updateTaskStatus(indexPath: indexPath)
            self.fetchyData()
            tableView.reloadRows(at: [indexPath], with: .automatic)
        }
        taskStatusAction.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)
        deleteAction.backgroundColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)

        return [deleteAction, taskStatusAction]
    }



}

extension ViewController {

    func fetchData(completion: (_ complete: Bool) -> ()) {
         guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Task")
        do {
            taskArray = try  managedContext.fetch(request) as! [Task]
            print("Data fetched, no issues")
            completion(true)
        } catch {
            print("Unable to fetch data: ", error.localizedDescription)
            completion(false)
        }

    }

    func deleteData(indexPath: IndexPath) {
         guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
        managedContext.delete(taskArray[indexPath.row])
        do {
            try managedContext.save()
            print("Data Deleted")

        } catch {
            print("Failed to delete data: ", error.localizedDescription)

        }
    }

    func updateTaskStatus(indexPath: IndexPath) {
        guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
        let task = taskArray[indexPath.row]
        if task.taskStatus == true {
            task.taskStatus = false
        } else {
            task.taskStatus = true
        }
        do {
            try managedContext.save()
            print("Data updated")

        } catch {
            print("Failed to update data: ", error.localizedDescription)

        }

    }

}
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {


   func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        self.fetchyData()
        return taskArray.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        self.fetchyData()
        return taskArray[row].taskDescription.  //THE ERROR IS ON THIS LINE . Index Out OF Range//

    }

}

【问题讨论】:

  • 你的任务数组有多少个条目?
  • 这行代码在做什么?它将尝试选择第 1000 行...如果 taskarray 中没有该数字...这将崩溃
  • 那条线不是问题,我在按下按钮后将 cocoapod 用于无限的 UIpickerView,那是 1000,它工作正常。我的崩溃发生在线路上——返回 taskArray[row].taskDescription。仅当核心数据中没有任何内容时。当有数据时,它很好.. .
  • 在此行之前添加一个检查...如果 taskArray[row] 存在或不存在
  • jawadAli,谢谢你,这就是我的问题,我相信它是一个 IF 语句,但我对 Swift 非常陌生(只有 1 个月)而且我卡住了,我写了不同的 if 语句,但它们不是在职的。能不能给我一个先机?

标签: ios swift uipickerview


【解决方案1】:

如果您遇到 outOfIndex 崩溃,请在访问数组中的元素之前检查 Swift 中的一个很好的方法:

 let isIndexValid = taskArray.indices.contains(row) 

  if isIndexValid {
   return taskArray[row].taskDescription

   } else {
       return "default text"
   }

【讨论】:

  • 我添加了代码并且我得到了这个错误......条件绑定的初始化程序必须具有可选类型而不是“bool”......但至少它与过去 2 天的错误不同。谢谢你帮助我
  • 现在试一试......我的错......对不起......你不能添加如果让非可选:)
  • 它现在运行良好,非常感谢。我可以再问一个愚蠢的问题,我现在将花时间理解您的代码。但是说“默认文本”的那一行是什么“默认文本”?
  • 如果索引不存在,您要返回的任何文本...根据您的应用要求返回任何内容...它也可以是空字符串“”:)
  • 请也给我点赞
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2020-10-27
  • 1970-01-01
  • 2013-04-27
  • 2020-10-16
  • 1970-01-01
相关资源
最近更新 更多