UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Issue with using Directed Graph from a separate file in the ContentView

Forums > SwiftUI

My set-up is I have created a very basic Directed Graph (essentially just a linked list at this point) data-structure with a single Node struct. The Node struct as a mutating addChild method so I can add a Node as a child to another Node represented as an array of Node.

I have another struct where I build up the Directed Graph by initializing a bunch of nodes and then calling the addChild on each one to "connect" them. Then I return the root node, which through it's array of children Node's should, in theory, contain every other Node I created.

In the ContentView where I am defining the user interface I have a property set to the root Node. Then the user can go from one node to the next by clicking buttons (it gets the next node by taking the 0th element of the child array). The first one works fine, but the when I get to the second node it says index out of bounds, even though there is a corresponding addChild statement so it should have one element in the array. There is other data associated with the Node which does come through like text.

If there is some easy thing I'm missing please let me know! If there is not but you are still willing to help me out I can send you the code somehow or something. Thanks in Advance!

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.