2016-06-15 203 views
0

我想將數組值合併成一個字符串。 我的數組是像...將多個數組值組合成一個字符串值?

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

我需要像這樣...

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

回答

1

試試這個:

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]); 
} 

希望這有助於!

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]); 
} 

我已經測試此代碼。它工作完美。再次檢查並重新考慮問題。

+0

感謝您的回答... :) – NilamPari

+0

編碼快樂:) –

+0

,要是我有一個數組值的空白 這樣 的NSArray *數組1 = @ @「fizan」,@「」 @「彈簧」] ; NSArray * array2 = @ [@「round」,@「rectangle」,@「square」]; NSArray * array3 = @ [@「frame」,@「frame」,@「frame」]; 然後它給出錯誤,如 索引0超出空NSArray的界限 你能幫我解決這個問題嗎? – NilamPari

1

做這個

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

輸出將得到像

value = fizan nike pogo 

對於你的情況

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); 
    } 

輸出

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

檢查問題和輸出中的輸出 –

+0

@BhargavSoni嗨,現在我已經編輯了答案。 –

+0

,因爲你已經編輯了答案,它不會做任何改變; –

相關問題