Merge Objects in NSArray

I have an array like this:

NSArray *arr = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil]; 

how can i join the first element with second and third with fourth, etc.?

+4
source share
1 answer

As I understand it, this should lead to @"12",@"34",@"56"

 NSArray *arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil]; NSMutableArray *array2 = [NSMutableArray array]; [arr enumerateObjectsUsingBlock:^(NSString *string1, NSUInteger idx, BOOL *stop) { if (idx > 0 && idx %2 == 1) { NSString *string0 = [arr objectAtIndex:idx-1]; [array2 addObject:[NSString stringWithFormat:@"%@%@", string0, string1]]; } }]; NSLog(@"%@", array2); 

result:

 ( 12, 34, 56 ) 
+4
source

All Articles