//对数组中元素块操作 NSArray* array = @[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff",@"gg"]; [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%@",obj); }]; NSLog(@"-------------- "); //NSIndexSex 是取值范围 options为NSEnumerationReverse倒序,NSEnumerationConcurrent正序 [array enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 4)] options:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%@",obj); }]; NSLog(@"-----enumerateObjectsUsingBlock--------- "); _textfield1 = [[UITextField alloc]init]; _textfield2 = [[UITextField alloc]init]; _textfield3 = [[UITextField alloc]init]; _textfield4 = [[UITextField alloc]init]; _textfield5 = [[UITextField alloc]init]; _textfield6 = [[UITextField alloc]init]; NSArray* textfieldArray = @[_textfield1,_textfield2,_textfield3,_textfield4,_textfield5,_textfield6]; //可以利用此方法进行控件批量处理,不过不能进行创建处理,因为Array会报插入null元素的错。 [textfieldArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UITextField* textfield = obj; NSLog(@"%i",idx); textfield.tag = idx; textfield.text = [array objectAtIndex:idx]; NSLog(@"%@",textfield.text); textfield.delegate = self; }]; NSLog(@"text3 : %@",_textfield3.text); NSLog(@"-------indexOfObjectPassingTest------- "); //可以处理自己想要的元素,参数方法同上类似 NSInteger index = [textfieldArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { UITextField* textfield = (UITextField*)obj; NSLog(@"%i",textfield.tag); NSLog(@"%@",textfield.text); if([textfield.text isEqualToString:@"cc"]){ return YES; } return NO; }]; NSLog(@"%i\n------------------------",index);