app4g
app4g

Reputation: 850

How to decode passed (nested) Array from didSelectRowAt to another TabBar

I'm stuck at figuring out how to pass an entire JSON decoded Array(I'm not sure if it's called an array?) from one TabBar to another.

I finally managed to figure out how to pass the selectedRow data to another tab using this YouTube https://www.youtube.com/watch?v=GL8-eM93EvQ but I am only successful in passing some items instead of the entire decoded JSON.

my current JSON looks like this and I have the JSON as a decoable and have done the unkeyedContainer for the intervals (The values inside the interval are time(x) and value(y):)

    [
  {
    "id":1,
    "title":"AirBender",
    "durationMinutes": 25,
    "intervals:":[
      [0.00,  50],
      [10.00,  55],
      [10.00,  73]
    ]
  }
]

In the table view TabBar, the didSelectRowAt contains:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    let tabbar = tabBarController as! BaseTBController
    tabbar.workoutTitle = jsonErgWorkouts[indexPath.row].title <- This works great
//    tabbar.workoutIntervals = jsonErgWorkouts[indexPath.row] <- This doesn't work
  }

This is what I have in the (main) TabBarController Class

import UIKit

class BaseTBController: UITabBarController {

  var workoutTitle: String = "Select a Workout"
  // var workoutIntervals: [workoutList] = [] <- I don't know what to declare here
  
    override func viewDidLoad() {
        super.viewDidLoad()
        print("BaseTBController title:\(workoutTitle)")
    }
}

I even tried to declare it as part of the Decodable struct

    struct workoutList: Decodable {
      let id: Int
      let title: String
      let durationMinutes: Double
      let intervals: [workoutIntervals]
    }

struct workoutIntervals: Decodable {
  let t: Double // time in min
  let w: Int    // %FTP
  
  init(from decoder: Decoder) throws {
    var arrayContainer = try decoder.unkeyedContainer()
    t = try arrayContainer.decode(Double.self)
    w = try arrayContainer.decode(Int.self)
  }
}

edit: I just tried to put the workoutIntervals as ANY]

var workoutIntervals = [Any]()

and it worked to send the workoutIntervals to the other tab. However, now I don't know how I can access the data inside as it's not tied to the decodable struct.

printing out the values via:

    for (intervalIndex, workoutIntervals) in tabbar.workoutIntervals.enumerated() {
  print("intIndex:\(intervalIndex) time:\(workoutIntervals)")

The result is:

intIndex:0 time:workoutIntervals(t: 0.0, w: 50)
intIndex:1 time:workoutIntervals(t: 10.0, w: 55)
intIndex:2 time:workoutIntervals(t: 10.0, w: 73)

but like I said, I don't know how to get to the t: and w: since it's not tied to the struct.

Thanks!!

Upvotes: 0

Views: 45

Answers (1)

Shahin
Shahin

Reputation: 885

Thanks for the information. So the BaseTBController acts like the model part for the application. is that right? So it would be the singleton model that tabs can request information from or listen for changes. In my opinion, To notify the other view controller in another tab about the selected item, You may use delegate, notifications or PublishSubject if you are using Combine or RxSwift.

Then doesn't matter how the data structure is, you can pass whatever you need to any of those tabs. (like the struct itself or the array within). Is this clear enough?

Upvotes: 0

Related Questions