How to create a recursive closure?

It seems like it's impossible without intermediate functions. (Sorry, didn't found a similar question)

Recursive closure would create reference cycle, esp. when all reference to it is strong in current version of Swift. That's why it's not allowed (AFAIK).

Really. And there is no way to pass weak reference to the closure itself into the closure?

I don't think so. You can't even have weak var for closure.

In any case I'd suggest you create a local function for what you want to do.
Often time you'll find that closure's power of capturing variable doesn't bode well with recursive logic to begin with.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy