遍历数组

NSArray提供三种解决方案遍历对象列表:

1.内置循环for-each:

<span style="white-space:pre">	</span>for(NSMutableString *s in listOfObjects){
    <span style="white-space:pre">	</span>NSLog(@"%@",[s lowercaseString]);
<span style="white-space:pre">	</span>}

2.使用makeObjectsPerformSelector:或makeObjectsPerformSelector:withObject: 方法:

- (void)makeObjectsPerformSelector:(SEL)aSelector;

- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;

 

这是 NSArray和NSSet的两个方法,相信大家很少用,它类似于 for循环,但有效率高于for循环

makeObjectsPerformSelector:类似于NSNotifation机制,并发的执行同一件事,不能像for循环那样区别对待

所以参数 argument 必须是非基本类型 ,如果要是用基本类型 请转换程 NSNumber 或者NSValue

3.通过Blocks的enumerateObjectsUsingBlock方法:

<span style="white-space:pre">	</span>[listOfObjects enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop)
<span style="white-space:pre">	</span>    NSLog(@"object(%lu)'s description is %@",idx,[obj description]);
<span style="white-space:pre">	</span>];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值