Nilam Goswami Nilam Goswami - 5 months ago 22
Objective-C Question

combine multiple array values into one string value?

I want to combine the array values into one string.
my arrays are like...

array1=[@"fizan",@"nike",@"pogo"];
array2=[@"round",@"rectangle",@"square"];
array3=[@"frame",@"frame",@"frame"];


I need like this...

value1 = fizan round frame
value2 = nike rectangle frame
value3 = pogo square frame

Answer

try this:

NSArray *array1= @[@"fizan",@"nike",@"pogo"];
NSArray *array2= @[@"round",@"rectangle",@"square"];
NSArray *array3= @[@"frame",@"frame",@"frame"];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:@[array1,array2,array3]];
NSMutableArray *output = [[NSMutableArray alloc] init];
NSString *a;
NSInteger count = array.count;

for (int i = 0; i<array1.count; i++) {
    a = @"";
    for (int j = 0; j<count; j++) {
        a = [a isEqualToString: @""] ? [NSString stringWithFormat:@"%@",[[array objectAtIndex:j] objectAtIndex:i]] : [NSString stringWithFormat:@"%@ %@",a,[[array objectAtIndex:j] objectAtIndex:i]];
    }
    [output addObject:a];
}

for (int i = 0; i < output.count; i++) {
    NSLog(@"value %i -> %@",i+1,output[i]);
}

Hope this helps!

UPDATE:

NSArray *array1= @[@"fizan",@"",@"pogo"];
NSArray *array2= @[@"round",@"rectangle",@"square"];
NSArray *array3= @[@"frame",@"frame",@"frame"];

NSMutableArray *array = [[NSMutableArray alloc] initWithArray:@[array1,array2,array3]];
NSMutableArray *output = [[NSMutableArray alloc] init];
NSString *a;
NSInteger count = array.count;

for (int i = 0; i<array1.count; i++) {
    a = @"";
    for (int j = 0; j<count; j++) {
        a = [a isEqualToString: @""] ? [NSString stringWithFormat:@"%@",[[array objectAtIndex:j] objectAtIndex:i]] : [NSString stringWithFormat:@"%@ %@",a,[[array objectAtIndex:j] objectAtIndex:i]];
    }
    [output addObject:a];
}

for (int i = 0; i < output.count; i++) {
    NSLog(@"value %i -> %@",i+1,output[i]);
}

I have tested this code. It works perfect. Check again and reconsider the issue.

Comments