# Moving through child list of list

**URL:** <https://forums.swift.org/t/moving-through-child-list-of-list/49377>\
**Category:** Using Swift\
**Tags:** swift5, codable, tableview\
**Created:** [June 8, 2021, 6:24pm UTC](https://forums.swift.org/t/moving-through-child-list-of-list/49377 "2021-06-08T18:24:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AliJalil](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alijalil/32/17907_2.png) [@AliJalil](https://forums.swift.org/u/AliJalil)\
**Post date:** [June 8, 2021, 6:24pm UTC](https://forums.swift.org/t/moving-through-child-list-of-list/49377/1 "2021-06-08T18:24:07Z")

</div>

I have the following structure:

```swift
struct SoundModel:Codable
{
    var sort : String? = ""
    var type : String? = ""
    var name : String? = ""
    var pic_file_path : String? = ""
    var text : String? = ""
    var related_item : [SoundModel]? = nil
}
var soundModel :SoundModel = SoundModel()
var dataSourceSounds :[SoundModel] = [SoundModel()]
 self.dataSourceSounds = soundModel.related_item!

```

the above `soundModel` can be represented as tree where there is a root with a children of `related_item`, what I force when I implement this structure in an iOS app with using a tableview, I couldn't figure out how can I traverse (showing) `related_item` of it's child and how to get back to the parent from child,

when first click of an item in my table view I get

```swift
dataSourceSounds = dataSourceSounds[indexPath.row].related_item

```

and in second click get

```swift
 dataSourceSounds = dataSourceSounds[indexPath.row].related_item

```

where I'll get second level of `related_item`  
And so on,  
How can I implement such idea to make it capable to click a button to go back a level?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![QuinceyMorris](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@QuinceyMorris](https://forums.swift.org/u/QuinceyMorris)\
**Post date:** [June 9, 2021, 12:17am UTC](https://forums.swift.org/t/moving-through-child-list-of-list/49377/2 "2021-06-09T00:17:07Z")

</div>

There is no "best" answer here. What is best for you will depend on a lot of other factors in your app, and your own coding style.

One approach is to use a class for `SoundModel` instead of a struct. Remember that there can be many copies of a value of a struct type, so the concept of "parent" and "child" gets a bit murky there.

With classes, the values are unique, so parent/child relationships are easier to grasp.

If you change `struct` to `class`, you can add a weak link from the child back to the parent (weak to avoid annoying retain cycles):

```swift
class SoundModel:Codable
{
    weak var parent: SoundModel?
    …
}

```

The only problem is that you would need to set the reference from the child to the parent separately from the JSON decoding, and would have to exclude the `parent` property from the JSON encoding. So you'd need a customization of `Codable`.

There are other approaches, too. You could keep a `struct SoundModel`, but embed it in an instance of a `TreeNode` class of some kind. Or you could stay completely with structs, and use some kind of "tree locator index" to navigate the tree of structs, which would allow you move up as well as down.
