I am somewhat of a novice at Swift, and am having a difficult time understanding the logical flow in which things are being processed. There are a couple of things in my program that seem to be running in a sequence that I'm not expecting, in In the following code, I need to perform the function "getValues" (when the user has selected a row from my summary table)
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
tableView.deselectRow(at: indexPath, animated: true)
gameNo = indexPath.row
getValues()
vRatings.append(defRat[0])
hRatings.append(defRat[1])
self.performSegue(withIdentifier: "gameSelected", sender: self)
}
func getValues() { // (it is here where the array "defRat" gets populated
However, when I walk through the code in debug mode, the call to getValues just gets skipped over. Coming from a background of traditional coding (COBOL, FORTRAN, etc), this makes no sense to me. The program is blowing up on illegal indexing, because the 'defRat' array was never populated.
Hopefully, there is a simple answer...many thanks in advance.
Instead of
func getValues() {, dofunc getValues() -> [String] {. (Replace String with whatever the array is in the array.) Then, instead of updating defRat, you can return an array of String (or whatever type defRat is). In thetableViewfunction, replacegetValues()withvar exampleVar = getValues(), and you can replacedefRatwhen you append withexampleVar. All together, it should look like this: