5

NSDictionary を含む NSArray で高速列挙を使用することは可能ですか?

私はいくつかの Objective C チュートリアルを実行しています。次のコードは、コンソールを GDB モードに切り替えます。

NSMutableArray *myObjects = [NSMutableArray array];
NSArray *theObjects = [NSArray arrayWithObjects:@"easy as 1",@"easy as two", @"Easy as Three"];
NSArray *theKeys    = [NSArray arrayWithObjects:@"A",@"B",@"C"];    
NSDictionary *theDict = [NSDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
[myObjects addObject:theDict];

for(id item in myObjects)
{
    NSLog(@"Found an Item: %@",item);
}

高速列挙ループを従来のカウント ループに置き換えると、

int count = [myObjects count];
for(int i=0;i<count;i++)
{
    id item;
    item = [myObjects objectAtIndex:i];
    NSLog(@"Found an Item: %@",item);
}

アプリケーションはクラッシュせずに実行され、ディクショナリがコンソール ウィンドウに出力されます。

これは高速列挙の制限ですか、それとも言語の微妙な部分が欠けていますか? このようにコレクションをネストする場合、他に注意点はありますか?

おまけとして、GDB を使用してこれを自分でデバッグするにはどうすればよいですか?

4

1 に答える 1

11

おっと!arrayWithObjects:nilで終了する必要があります。次のコードは問題なく実行されます。

NSMutableArray *myObjects = [NSMutableArray array];
NSArray *theObjects = [NSArray arrayWithObjects:@"easy as 1",@"easy as two", @"Easy as Three",nil];
NSArray *theKeys    = [NSArray arrayWithObjects:@"A",@"B",@"C",nil];    
NSDictionary *theDict = [NSDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
[myObjects addObject:theDict];

for(id item in myObjects)
{
    NSLog(@"Found an Item: %@",item);
}

従来のループを使用するとこのエラーが隠された理由がわかりません。

于 2010-02-19T23:20:42.680 に答える