TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Recursion in a Table

Forums > SwiftUI

Using SwiftData for a MacOS app I have a working Table but I'd like to add recursion to the rows so instead of stopping at the first child level I can get to grandchildren, etc. Tried creating a separate func that calls itself but cannot get working syntax, probably because this is happening inside a DisclosureTableRow. Instead of calling TableRow(childAccount) I'd like to be able to call a recursive function which determines if each child is treated as a parent or not until there are no more grandchildren.

Each Account class object has .parent: Account, .children: [Account], and .isExpanded:Bool (required by the DisclosureTableRow but not changed here). This is the working non-recursive code:

First the singleton theMotherAccount is at the top level then we iterate over an array of other accounts, theAccounts, showing only those that are not themselves children. Any children are then surfaced as part of another DisclosureTableRow.

               DisclosureTableRow(theMotherAccount, isExpanded: $theMotherAccount.isExpanded) {
                  ForEach(theAccounts.sorted(using: sortOrder)) { account in
                    // Make this account bindable so can use .isExpanded directly
                    @Bindable var account = account
                    // Check if the account is not a child of any other account to avoid duplcates,
                    if !theAssetAccounts.contains(where: { $0.children?.contains(account) ?? false }) {
                      // If the account has children, display them in a DisclosureTableRow…
                      if let children = account.children, !children.isEmpty {
                        DisclosureTableRow(account, isExpanded: $account.isExpanded) {
                          ForEach(children) { childAccount in
                            TableRow(childAccount)
                          }
                        }
                      } else {
                        // …or if the account has no children, display it in a simple TableRow
                        TableRow(account)
                      }
                    }
                  }
                }

I thought I could just create a recursive func to return either a DisclosureTableRow or a TableRow but have not been able to find acceptable syntax. What is the right way to do this?

1      

This is what I thought ought to work

func recursiveAccountRow(account: Account) -> any TableRowContent {
  if let children = account.children, !children.isEmpty {
    return DisclosureTableRow(account, isExpanded: Bindable(account).isExpanded) {
      ForEach(children) { child in
        recursiveAccountRow(account: child)
      }
    }
  } else {
    return TableRow(account)
  }
}

but the recursiveAccountRow(account: child) prompts a not so intelligible error "No exact matches in reference to static method 'buildExpression'"

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.