is there anything in Objective-C similar to C# yield return
?
No, there is nothing in Objective-C that would let you built an iterable solution that easily.
In general, fast enumeration in Objective-C is built using an entirely different mechanism from C#, Java, or C++. Adopting the protocol is relatively complex, especially compared to C# with its yield return
, though it is certainly doable.
I found that Objective-C blocks provide a usable alternative to fast enumeration. Consider implementing a block-based enumeration instead of fast enumeration - it lets you program your own API using the style similar to yield return
. On the flip side, the clients of your API would need to supply a block to use your enumeration. This is not ideal, but usable, especially for complex enumerators, such as ones based on trees.