遍历数组—三种内置的方法

    NSMutableString *string1 = [NSMutableString initWithFormat:@"Tom"];
        NSMutableString *string2 = [NSMutableString initWithFormat:@"Harry"];
        NSMutableString *string3 = [NSMutableString initWithFormat:@"Jack"];
        
        NSArray *array = [NSArray arrayWithObjects:string1,string2,string3, nil];
        
        way1- 枚举
        // 创建的 s 的类类型必须和需要被枚举的数组中的元素的类型保持一致
        for ( NSString *s in array ){
            NSLog(@"The string is %@.", s);
        }
        
        结果:
        The string is Tom.
        The string is Harry.
        The string is Jack.
        
        way2- 使用 selector (选择器)
        [array makeObjectsPerformSelector:@selector(appendingString:) withObject:@"-student"];
        
        way3- 使用块定义
        [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
			NSLog(@"The object(%lu)'s description is %@.", idx, [obj description]);
        }];
        
        在方法2 执行后,再执行方法3的结果:
        The object(1)'s description is Tom-student.
        The object(2)'s description is Harry-student.
        The object(3)'s description is Jack-student.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值