简体   繁体   English

在NSArray中连接对象

[英]join objects in NSArray

i have an array like this: 我有一个像这样的数组:

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

how i can join the first element with the second and the third with the fourth and so on? 我如何加入第一个元素与第二个和第三个与第四个,依此类推?

As I understand, it should result into @"12",@"34",@"56" 据我所知,它应该导致@"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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM