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

来源:互联网 发布:cf网络异常的解决办法 编辑:程序博客网 时间:2024/06/08 02:52
    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.

0 0
原创粉丝点击