我从核心数据填充 PickerView 并收到错误 Index Out Of Range Swift 5
Im populating a PickerView from Core data and getting the error Index Out Of Range Swift 5
我是 swift 的新手,我正在尝试了解使用 CoreData 填充 UIPickerView。在观看了有关使用 UITableView 执行此操作的视频后,我为 UIPickerView 实现了相同的代码。代码工作正常,直到我从 CoreData 中删除所有内容然后它因错误索引超出范围而崩溃。
我知道崩溃的代码行,但我知道如何修复它。我在这里阅读了很多关于索引超出范围的问题,但我看不到任何相同的东西。
这是代码,table查看代码来自视频,我理解其中的 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//
}
}
如果您遇到 outOfIndex 崩溃,请在访问数组中的元素之前检查
Swift中的绝妙方法:
let isIndexValid = taskArray.indices.contains(row)
if isIndexValid {
return taskArray[row].taskDescription
} else {
return "default text"
}
我是 swift 的新手,我正在尝试了解使用 CoreData 填充 UIPickerView。在观看了有关使用 UITableView 执行此操作的视频后,我为 UIPickerView 实现了相同的代码。代码工作正常,直到我从 CoreData 中删除所有内容然后它因错误索引超出范围而崩溃。
我知道崩溃的代码行,但我知道如何修复它。我在这里阅读了很多关于索引超出范围的问题,但我看不到任何相同的东西。
这是代码,table查看代码来自视频,我理解其中的 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//
}
}
如果您遇到 outOfIndex 崩溃,请在访问数组中的元素之前检查 Swift中的绝妙方法:
let isIndexValid = taskArray.indices.contains(row)
if isIndexValid {
return taskArray[row].taskDescription
} else {
return "default text"
}