¿Es posible usar la enumeración rápida con un NSArray que contiene un NSDictionary?
Estoy ejecutando algunos tutoriales de Objective C, y el siguiente código coloca la consola en modo 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);
}
Si reemplazo el bucle de enumeración rápida con un bucle de conteo tradicional
int count = [myObjects count];
for(int i=0;i<count;i++)
{
id item;
item = [myObjects objectAtIndex:i];
NSLog(@"Found an Item: %@",item);
}
La aplicación se ejecuta sin bloqueo y el diccionario se envía a la ventana de la consola.
¿Es esta una limitación de la enumeración rápida o me estoy perdiendo algo del idioma? ¿Hay otras trampas al anidar colecciones como esta?
Para obtener puntos de bonificación, ¿cómo podría usar GDB para depurar esto yo mismo?
Respuestas
11 para la respuesta № 1Ups! arrayWithObjects:
necesita ser terminado en cero. El siguiente código corre muy bien:
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);
}
No estoy seguro de por qué el uso de un bucle tradicional ocultó este error.