0

ツリー構造によるオブジェクト リストがあります。サンプル コードは次のとおりです。

Model  model1 = [[Model alloc]init];
model1.name = @"ABC";


Model *model2 = [[Model alloc]init];
model2.name = @"DEF";


Model *model3 = [[Model alloc]init];
model3.name = @"GHI";

Model *model4 = [[Model alloc]init];
model4.name = @"JKL";

[model3.arr addObject:model4];
[model2.arr addObject:model3];
[model1.arr addObject:model2];

他のすべてのオブジェクトをループせずに特定のオブジェクトを取得する方法はありますか? 前もって感謝します。

4

1 に答える 1

0

Rather than using a array (I assume that is what Model.arr is) to hold the sub-models, use a dictionary as this provides fast look-up of objects without the need to trawl through the complete collection.

However in order to use a dictionary, you are going to need to supply a unique key in order to access the object.

I would also recommend not exposing arr as that means implementation changes will break dependent code; instead provide methods to add, fetch and remove sub-models:

- (void)addModel:(Model *)model forKey:(NSString *)key;
- (Model *)modelForKey:(NSString *)key;
- (void)removeModelWithKey:(NSString *)key;
于 2013-02-28T10:53:51.710 に答える