NSArray:
//----- 传统方式
for (int i=length-1; i>=0; i--)
for (int i=length-1; i>=0; i--)
{
NSLog(@"%d = %@",i,[mutableArray objectAtIndex:i]);
}
NSLog(@"%d = %@",i,[mutableArray objectAtIndex:i]);
}
//----- 高效方式
for(NSString *str in mutableArray)
for(NSString *str in mutableArray)
{
NSLog(@"obj =%@",str);
}
NSLog(@"obj =%@",str);
}
NSDictionary(注:遍历时为乱序):
1)一般的遍历
NSArray *keys4 = [dic4 allKeys];
for(int i=0;i<[dic4 count];i++)
NSArray *keys4 = [dic4 allKeys];
for(int i=0;i<[dic4 count];i++)
{
NSLog(@"dic4 key = %@,value=%@",[keys4 objectAtIndex:i],[dic4 objectForKey:[keys4 objectAtIndex:i]]);
}
NSLog(@"-----------------------");
NSLog(@"dic4 key = %@,value=%@",[keys4 objectAtIndex:i],[dic4 objectForKey:[keys4 objectAtIndex:i]]);
}
NSLog(@"-----------------------");
2)高效的for
for (NSString *key in dic4){
NSLog(@"dic4 key = %@ ,value = %@",key,[dic4 objectForKey:key]);
}
NSLog(@"-----------------------");
3)使用枚举进行遍历
NSEnumerator *enum1 = [dic4 keyEnumerator];
//获取key,如果不为空,则进行偏移
id key = [enum1 nextObject];
while (key) {
NSLog(@"key = %@ ,value = %@ ",key,[dic4 objectForKey:key]);
key = [enum1 nextObject];
}
NSEnumerator *enum1 = [dic4 keyEnumerator];
//获取key,如果不为空,则进行偏移
id key = [enum1 nextObject];
while (key) {
NSLog(@"key = %@ ,value = %@ ",key,[dic4 objectForKey:key]);
key = [enum1 nextObject];
}