I have parsed a json file with the following structure into a table view succesfully.
import Foundation
struct ActionResult: Codable {
let data3: [Datum]
}
struct Datum: Codable {
let actionGoal, actionGoalDescription, actionGoalImage: String
let actions: [Action]
}
struct Action: Codable {
let actionTitle: String
let actionID: Int
let select, completed, favorite: Bool
let actionType, actionDescription, actionTips, actionImage: String
let actionSponsor, actionSponsorURL: String
Now I am preparing a segue to a detail ViewController but is giving me an error.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ActionDetailViewController {
destination.action = result?.data3.actions[(tableView.indexPathForSelectedRow?.row)!]
Error description. Value of type '[Datum]' has no member 'actions'
Who can help me?
You already posted the same question a moment ago… It is about segue, not seque.
Problem is
destination.action = result?.data3.actions[(tableView.indexPathForSelectedRow?.row)!]
data3 is [Datum], not Datum. Hence, it has no actions property.
Which datum in data3 do you want to use ?
- you have to define this index (take care it is consistent with the number of items in data3):
var indexData = 0
-
Set it somewhere in code (in didSelectRowAt or elsewhere)
-
Use it in prepare:
destination.action = result?.data3[indexData].actions[(tableView.indexPathForSelectedRow?.row)!]
Note: when you paste code use "Paste and Match Style and the use code formatter tool:
import Foundation
struct ActionResult: Codable {
let data3: [Datum]
}
struct Datum: Codable {
let actionGoal, actionGoalDescription, actionGoalImage: String
let actions: [Action]
}
struct Action: Codable {
let actionTitle: String
let actionID: Int
let select, completed, favorite: Bool
let actionType, actionDescription, actionTips, actionImage: String
let actionSponsor, actionSponsorURL: String
var indexData = 0
// Now I am preparing a segue to a detail ViewController but is giving me an error.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
indexData = 0 // Initialise here or elsewhere, before segue.
performSegue(withIdentifier: "showDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ActionDetailViewController {
destination.action = result?.data3[indexData].actions[(tableView.indexPathForSelectedRow?.row)!]