NSObject *obj=[[NSObject alloc]init];
NSArray *array=[[NSArray alloc] initWithObjects:@"abc",obj,@"cde",@"opq",@25, nil];
//方法1 随便
//int i=0;
//int len=(int)array.count;
//for(;i<len;++i){
// NSLog(@"method1:index %i is %@",i,[array objectAtIndex:i]);
//}
/*结果:
method1:index 0 is abc
method1:index 1 is <NSObject: 0x100106de0> method1:index 2 is cde
method1:index 3 is opq
method1:index 4 is 25
*/
//方法2 好high
//for(id obj in array){
// NSLog(@"method2:index %zi is %@",[array indexOfObject:obj],obj);
//}
/*结果:
method2:index 0 is abc
method2:index 1 is <NSObject: 0x100602f00> method2:index 2 is cde
method2:index 3 is opq
method2:index 4 is 25
*/
//方法3,利用代码块方法 不知道
//[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// NSLog(@"method3:index %zi is %@",idx,obj);
// if(idx==2){//当idx=2时设置*stop为YES停止遍历
// *stop=YES;
// }
//}];
/*结果:
method3:index 0 is abc
method3:index 1 is <NSObject: 0x100106de0> method3:index 2 is cde
*/
//方法4,利用迭代器 推荐
//NSEnumerator *enumerator= [array objectEnumerator];//获得一个迭代器
NSEnumerator *enumerator=[array reverseObjectEnumerator];//获取一个反向迭代器 //
//NSLog(@"all:%@",[enumerator allObjects]);//获取所有迭代对象,注意调用完此方法迭代器就遍历完了,下面的nextObject就没有值了
id obj2=nil;
while (obj2=[enumerator nextObject]) {
if([obj2 length] > 2){//只是一个示例,可以加入条件进行选择
NSLog(@"method4:%@",obj2);
}
}
/*结果:
method4:25
method4:opq
method4:cde
method4:<NSObject: 0x100106de0> method4:abc
*/