例如,假设我有一个NSArray
包含一堆对象的NSString
对象:
NSArray *games = [NSArray arrayWithObjects:@"Space Invaders", @"Dig Dug", @"Galaga", nil];
这样做有什么好处和坏处:
for (id object in games) {
NSLog(@"The name of the game is: %@", object);
NSLog(@"The name is %d characters long.", [object length]);
// Do some other stuff...
}
相对:
for (NSString *name in games) {
NSLog(@"The name of the game is: %@", name);
NSLog(@"The name is %d characters long.", [name length]);
// Do some other stuff...
}