いくつかのオブジェクトコンポジションを持つモデルクラスがありますが、このためのイテレータを作成するための最良の方法がわかりません。問題をより詳細に確認するために、階層(半擬似コード)を次に示します。
ルートクラス:
MYEntity : NSObject
@property int commonProperty;
@property NSArray *childs; //Childs of any kind.
いくつかの具体的なサブクラス:
MYConcreteStuff : MYEntity
@property int number;
MYConcreteThing : MYEntity
@property NSString *string;
そして、具体的なコレクションを持つルートオブジェクト:
MYRoot : MYEntity
@property MYEntity *stuff; //Collect only stuff childs here.
@property MYEntity *things; //Collect only thing childs here.
これで、次のように、コレクションのクールなメンバーアクセサーを(MYEntityで)作成できます。
-(MYEntity*)entityForIndex:(int) index
{
if ([self.childs count] > index)
return [self.childs objectAtIndex:index];
return nil;
}
そして、ルートオブジェクト用のさらにクールで適切なタイプのキャストメンバーアクセサー。
-(MYConcreteThing*)thingForIndex:(int) index
{
if ([self.things count] > index)
return (MYConcreteThing*)[self.things entityForIndex];
return nil;
}
しかし、私はそのようなコレクションのためにいくつかのワンライナーイテレーターを書く方法がわかりません。志望のクライアントコードは次のようなものです。
for (MYConcreteThing *eachThing in myRoot.things)
eachThing.string = @"Success."; //Set "thingy" stuff thanks to the correct type.
私はブロックを使用することを考えていますが、よりクリーンなカットソリューションがあるかもしれません。何かアイデア/経験はありますか?