I have a model class with some composition of objects, and I donβt know how best to write iterators for this. To see the problem in more detail, here is the hierarchy (semi-pseudo-code):
Root Class:
MYEntity : NSObject @property int commonProperty; @property NSArray *childs;
Some specific subclasses:
MYConcreteStuff : MYEntity @property int number; MYConcreteThing : MYEntity @property NSString *string;
And the root object with specific collections:
MYRoot : MYEntity @property MYEntity *stuff; //Collect only stuff childs here. @property MYEntity *things; //Collect only thing childs here.
Now I can write cool access controls for collections (in MYEntity), for example:
-(MYEntity*)entityForIndex:(int) index { if ([self.childs count] > index) return [self.childs objectAtIndex:index]; return nil; }
And even more cool, cool cast accessors for the root object.
-(MYConcreteThing*)thingForIndex:(int) index { if ([self.things count] > index) return (MYConcreteThing*)[self.things entityForIndex]; return nil; }
But I have no idea how to write some oneliner iterators for such collections. The wannabe client code looks something like this:
for (MYConcreteThing *eachThing in myRoot.things) eachThing.string = @"Success.";
I am thinking about using blocks, but there might be a cleaner solution. Any ideas / experience?
iterator collections casting ios objective-c
Geri
source share