0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

if i have any of one array value is blank like this NSArray *array1= @[@"fizan",@"",@"pogo"]; NSArray *array2= @[@"round",@"rectangle",@"square"]; NSArray *array3= @[@"frame",@"frame",@"frame"]; then it gives error like index 0 beyond bounds for empty NSArray can you help me solve this problem?
1

Do this

NSArray *array1 = @[@"fizan", @"nike", @"pogo"];
NSString *value = [array1 componentsJoinedByString:@" "];
NSLog(@"value = %@", value);

Output will get like

value = fizan nike pogo

For your case

    NSArray *completeArray = @[@[@"fizan",@"nike",@"pogo"], @[@"round",@"rectangle",@"square"], @[@"frame",@"frame",@"frame"]];

    NSMutableArray *resultArray = [NSMutableArray array];

    unsigned long count = 1;

    for (int i = 0; i< count; i++) {
        NSMutableArray *listArray = [NSMutableArray array];
        for (NSArray *itemArray in completeArray) {
            count = MAX(count,itemArray.count);
            if (i < itemArray.count) {
                [listArray addObject:itemArray[i]];
            }
        }
        [resultArray addObject:listArray];
    }

    for (NSArray *itemArray in resultArray) {
        NSString *value = [itemArray componentsJoinedByString:@" "];
        NSLog(@"value = %@", value);
    }

output

 value = fizan round frame
 value = nike rectangle frame
 value = pogo square frame

3 Comments

check the output in the question and your output
as you have edited the answer, it won't made any change;
your output is fizan nike pogo but the question suggests that it need fizan round frame

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.