0

There's a API callback returns a Json format result. The requirement in short is that I need to keep calling this API and keeps implement Breadth First Search on the results it returns.

Imaging it's a map with many nodes and connections. Every time I call API call for a node, it gonna return me list of its connected nodes. All I need now, is an array, which saves all the node that has been visited to avoid repeated visits.

But this is Swift and I am new to it. I was using Array and pass as inout inside the completion handler. But there's an error: escaping closures can only capture inout parameters explicitly by value which means I cannot do it like this.

Now you may ask why the array I have has to be stored by reference. Because the API call is async, which means I have to wait until it comes back to keep progressing Breadth First Search, means I have to pass this array by reference in order to do the recursion.

What other solutions I may have?

Tony Lin
  • 922
  • 6
  • 25
  • I guess this is what you looking for-: http://stackoverflow.com/questions/24647406/how-to-use-completionhandler-closure-with-return-in-swift – Tushar Sharma Mar 25 '17 at 18:16

1 Answers1

0

Swift Arrays are value types (not reference types) so you will need to store you array in an object. You can then pass the object to your handler and set the array content inside the object which is carried as a reference in the closures.

Alain T.
  • 40,517
  • 4
  • 31
  • 51