An example of accepting / implementing a quick enumeration for my class?

I am trying to figure out how to adopt a fast enumeration protocol (under iOS / target C) for the class I'm creating. I read the Apple docs section, but ... I didn't quite understand it!

Does anyone have an example code I could look at?

What I'm trying to do: I have an array of objects over which I want the sender to iterate. The sender wants to use the built-in design. Trick - I do not want the sender to see all the objects in the array, because some of them are not valid in the application context.

In other words, I want my iterator to return a subset of the objects in the array that match certain criteria.

I would prefer not to create new arrays in this process so as not to slow down the work.

+6
source share
3 answers

Apple's own FastEnumerationSample demonstrates this pretty well. Of his sounds, you may not have seen it yet.

There is also this blog post .

+8
source

A much better explanation is here:

http://www.cocoawithlove.com/2008/05/implementing-countbyenumeratingwithstat.html

NB: Apple's source code is technically correct, but poorly explained, and mostly useless if you don't like playing β€œguess what the programmer thought / smoked that day.”

Liekwise, Mike Ashe's message is correct and useful after you know how to do it, but awful, as a starting point.

+2
source

I don’t know what you mean by "sender wants to use the for-in construct". The result you are asking for is likely to be better achieved with indexesOfObjectsPassingTest:, which will give you a set of indices of all the elements in your array that will pass any test that you set up for it.

You can also use filterArrayUsingPredicate: to get a new array that is a subset of your original - I know you said you don't want to create new arrays, but if you want to return a subset of your array, you need to create a new array.

0
source

Source: https://habr.com/ru/post/917539/


All Articles