1.数组的快速枚举
例:NSArray *array = [NSArray arrayWithObjects: @"pig", @"fox", @"cat", @"dog", @"bear", nil];
for (NSString *str in array){
// in 的右边是集合,左边是集合中元素的类型
NSlog(@"str = %@", str);
}
2.字典的快速枚举
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"水杯", @"num1", @"茶壶", @"num2", @"桌子", @"num3", @"手机", @"num4", nil];
for(NSString *key in dic){
// in 右边是被枚举的对象,左边是被枚举的类型
NSLog(@"key = %@", key);
NSLog(@"value = %@", [dic valueForKey:key]);
}
3.集合的快速枚举
NSSet n*set = [NSSet setWithObjects:@"花都", @"国花", @"牡丹", @"30min", nil];
for(NSString *string in set) {
NSLog(@"string = %@", string);
}
注意:假如一个不知道元素个数和类型的集合,如何快速枚举?
//id类型格式转化符%@
//id(通用类型)泛指所有对象类型,没有具体哪一种类型
for(id obj in set) {
NSLog(@"%@", obj);
}