Brian,
I am building on what I learned in the course and hit another road block and I think its due to another rookie mistake. I have three Core Data Entities named "CircuitSub", "Item", and "ItemSubCircuit". There is a one to one inverse relationship named "toCircuitSub" between "Item" and "ItemSubCircuit". I am successfully adding entries into all three Core Data Entities and displaying them within their specific VC within tableview cells that have swipe to "Edit" & "Delete" functionality.
Within the CreateItem VC I have a UIPickerView that is being populated via the "CircuitSub" entity and displaying the names of all the Sub Circuits. I am also adding an "Unassigned" selection option at row "0". When I save the new item, it successfully writes "Unassigned" or the selected Sub Circuit name to "Item.toCircuitSub" name attribute as expected.
The problem is when I select the swipe-able cell Edit action, I can not get the UIPickerview to display the "selected" Sub Circuit that was saved to the "Item.toCircuitSub" name attribute. I am 100% positive that there is a value for that attribute because I have a text field that is being successfully populated to verify that. If I comment out the "if let loadedSubCircuit" code, the info from the selected cell info loads just fine in the form and the UIPickerView displays all the Sub Circuit names as expected but is focused on the row zero "Unassigned" title. If I uncomment the "if let loadedSubCircuit" code, it crashes with an "Fatal error: Index out of range" on the "let s = circuitsSub[index]" line. I have literally worked on this issue for two days now and I can not put my finger on it. In addition to that issue, I would also like to sort (A>Z) the Sub Circuits within the UIPickerView while leaving the "Unassigned" at row zero. Do you have any pointers for that as well.
//======================================================================
//MARK: VARIABLES
//======================================================================
var delegateItems: CreateItemControllerDelegate?
var itemSubCircuit: ItemSubCircuit?
var itemElecSpecs: ItemElecSpecs?
var circuitsSub = [CircuitSub]() //Empty Array
var item: Item? {
didSet {
nameTextField.text = item?.name
if let imageData = item?.image {
itemImageView.image = UIImage(data: imageData)
setupCircularImageSytle()
}
circuitSubNameTextField.text = item?.toItemSubCircuit?.name
// START PROBLEM AREA
if let loadedSubCircuit = item?.toItemSubCircuit?.name {
var index = 0
repeat {
let s = circuitsSub[index] //GIVES ERROR: Thread 1: Fatal error: Index out of range
if s.name == loadedSubCircuit {
circuitSubPicker.selectRow(index, inComponent: 0, animated: false)
break
}
index += 1
} while (index < circuitsSub.count)
}
// END PROBLEM AREA
}
}
//======================================================================
//======================================================================
//MARK: VIEW CONTROLLER LIFECYCLES
//======================================================================
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
setupCancelButton()
self.circuitsSub = CoreDataManager.shared.fetchCircuitsSub()
self.circuitSubPicker.dataSource = self;
self.circuitSubPicker.delegate = self;
//circuitSubNameTextField.text = unassignedSubCircuitValue
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(handleSave))
view.backgroundColor = UIColor.colorThemeDkBlue
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationItem.title = item == nil ? "Create Item" : "Edit Item"
}
//======================================================================
//======================================================================
//MARK: UIPICKERVIEW
//======================================================================
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == circuitSubPicker {
return circuitsSub.count + 1
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == circuitSubPicker {
return row == 0 ? "Unassigned" : circuitsSub[row - 1].name
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == circuitSubPicker {
if row == 0{
print("Unassigned")
circuitSubNameTextField.text = "Unassigned"
} else {
print(circuitsSub[row - 1].name ?? "")
circuitSubNameTextField.text = circuitsSub[row - 1].name
}
}
}
//======================================================================