ios – i have a problem (I beleive that it is because of bad-threading), “Swift/ContiguousArrayBuffer.swift:588: Fatal error: Index out of range”

[ad_1]

here is a small description code:

  1. in function DidEndEditing I am passing the value of the textField to the object workTransactions[DaysSchedulesVC.dayIDHolder.dayID]

  2. DaysSchedulesVC.dayIDHolder.dayID (returns Int) also it is a static instance Struct so I can hold a value from the first view Controller to the next view Controller.

here is the problem:
when I run the code and move from the first to the second to the third VCs then I start typing and then when I do finish Editing and try to save the textfield value into the WorkTransactions object ,inside the function DidEndEditing, it says that
“Swift/ContiguousArrayBuffer.swift:588: Fatal error: Index out of range”
please see attached photo

//
//  WorkDetailsVC.swift
//  FlashHR
//
//  Created by Abdallah Mahameed on 3/23/22.
//

import UIKit

class WorkDetailsVC: UIViewController {
    @IBOutlet weak var viewBackground: UIView!
    @IBOutlet weak var tableView: UITableView!
    static var workTransactions: [WorkTansactions] = []
    var workingHoursPickerView = UIPickerView()
    var startTimePickerView = UIPickerView()
    var titles = [ "Project Name", "Contact Number", "Start Time", "Working Hours"]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        startTimePickerView.delegate = self
        startTimePickerView.dataSource = self
        startTimePickerView.tag = 1
        
        workingHoursPickerView.delegate = self
        workingHoursPickerView.dataSource = self
        workingHoursPickerView.tag = 2
        
        tableView.register(UINib(nibName: Constants.NibNames.workDetailsCell, bundle: nil) , forCellReuseIdentifier: Constants.Identifiers.workDetailsCellIdentifier)
        viewBackground.layer.cornerRadius = viewBackground.frame.size.height / 11
        
        //        print(EmployeesVC.empIDHolder.employeeID)
        
    }
    
    
    @IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true)
    }
    @IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
        
    }
}
//MARK: - TableView

extension WorkDetailsVC: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: Constants.Identifiers.workDetailsCellIdentifier, for: indexPath) as? WorkDetailsCell else {return UITableViewCell()}
        
        cell.titleLabel.text = titles[indexPath.row]
        cell.textField.delegate = self
        cell.textField.tag = indexPath.row
        
        switch indexPath.row {
        case 1:
            cell.textField.keyboardType = .numberPad
        case 2:
            cell.textField.inputView = startTimePickerView
        case 3:
            cell.textField.inputView = workingHoursPickerView
        default:
            break
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

//MARK: - TextFieldDelegate

extension WorkDetailsVC: UITextFieldDelegate {
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        
        switch textField.tag {
        case 0:
            //projectName
            WorkDetailsVC.workTransactions[DaysSchedulesVC.dayIDHolder.dayID].projectName = textField.text ?? ""
            //            
        case 1:
            //contactNo
            WorkDetailsVC.workTransactions[DaysSchedulesVC.dayIDHolder.dayID].contactNo = textField.text ?? ""
            //            
        case 2:
            //startTime
            WorkDetailsVC.workTransactions[DaysSchedulesVC.dayIDHolder.dayID].startTime = textField.text ?? ""
            //           
        case 3:
            //workingHours
            textField.text = String(WorkDetailsVC.workTransactions[DaysSchedulesVC.dayIDHolder.dayID].workingHours)
            //          
        default:
            return
        }
    }
}


//MARK: - Start Time PickerView & Working Hours PickerView

extension WorkDetailsVC: UIPickerViewDelegate, UIPickerViewDataSource{
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch pickerView.tag {
        case 1:
            return Constants.WorkDetailsVCConstants.startTime.count
        case 2:
            return Constants.WorkDetailsVCConstants.workingHours.count
        default:
            return 1
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        switch pickerView.tag {
        case 1:
            return Constants.WorkDetailsVCConstants.startTime[row]
        case 2:
            return String(Constants.WorkDetailsVCConstants.workingHours[row])
        default:
            return "Data Not Found"
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        switch pickerView.tag {
        case 1:
            WorkDetailsVC.workTransactions[DaysSchedulesVC.dayIDHolder.dayID].startTime = Constants.WorkDetailsVCConstants.startTime[row]
            //            pickerView.resignFirstResponder()
            tableView.reloadRows(at: [IndexPath.init(row: 2, section: 0)], with: .automatic)
        case 2:
            WorkDetailsVC.workTransactions[DaysSchedulesVC.dayIDHolder.dayID].workingHours = Constants.WorkDetailsVCConstants.workingHours[row]
            //            pickerView.resignFirstResponder()
            tableView.reloadRows(at: [IndexPath.init(row: 3, section: 0)], with: .automatic)
        default:
            return
        }
    }
}

I really appreciate your help.enter image description here

[ad_2]

Source link

Leave a Reply

Your email address will not be published.