How to handle circular reference here?

class Node {
    var successors: Array<Node>
    init(_ successors: Array<Node>) {
        self.successors = successors
    }
}
let a = Node([b])
let b = Node([a])

This innocent piece of code gives an error; error: circular reference it says. The weird thing is that it was repeated 5 times. :thinking:.
An attempt to put there weak/unowned didn't do anything. How to get rid of this error?

var a = Node([])
let b = Node([a])
a.successors = [b]

I'm assuming that this is in the context of main.swift or a playground

2 Likes

Thanks, that helped

I forgot to mention - I got rid of circular references between lines of code, but there is still a circular reference between objects in memory. It would easily lead to a memory leak.

1 Like

That's life

Terms of Service

Privacy Policy

Cookie Policy