I thought I knew how to use quick enumeration, but I donβt understand something about this. If I create three NSString objects and three NSNumber and put them in an NSMutableArray :
NSString *str1 = @"str1"; NSString *str2 = @"str2"; NSString *str3 = @"str3"; NSNumber *nb1 = [NSNumber numberWithInt:1]; NSNumber *nb2 = [NSNumber numberWithInt:2]; NSNumber *nb3 = [NSNumber numberWithInt:3]; NSArray *array = [[NSArray alloc] initWithObjects:str1, str2, str3, nb1, nb2, nb3, nil];
then I do a quick enumeration on all NSString objects, for example:
for (NSString *str in array) { NSLog(@"str : %@", str); }
In the console, I get this result:
2011-08-02 13:53:12.873 FastEnumeration[14172:b603] str : str1 2011-08-02 13:53:12.874 FastEnumeration[14172:b603] str : str2 2011-08-02 13:53:12.875 FastEnumeration[14172:b603] str : str3 2011-08-02 13:53:12.875 FastEnumeration[14172:b603] str : 1 2011-08-02 13:53:12.876 FastEnumeration[14172:b603] str : 2 2011-08-02 13:53:12.876 FastEnumeration[14172:b603] str : 3
I registered only NSString s, but I get a string for every object in the array, even NSNumber , and I don't understand why. Does fast enumeration always use every object contained in an array?
source share