2013-03-05 47 views
2

我使用以下代碼嘗試將數組輸出連接到NSString中。如何將NSArray輸出連接到以逗號分隔的NSString

NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:@"id"], nil]; 
NSString *joinedString = [array1 componentsJoinedByString:@","]; 

NSLog(@"joinedString is %@", joinedString); 

我想這個輸出連接字符串:加入字符串是55,56,57,66,88 ......等等......此刻的輸出是:

2013-03-05 13:13:17.052 [63705:907] joinedString is 55 
2013-03-05 13:13:17.056 [63705:907] joinedString is 56 
2013-03-05 13:13:17.060 [63705:907] joinedString is 57 
2013-03-05 13:13:17.064 [63705:907] joinedString is 66 
+2

joinedString = [joinedString stringByAppendingFormat:@「%@」,[array1 componentsJoinedByString:@「,」]]; – 2013-03-05 13:16:25

+0

爲什麼不去一個nsstring類別或函數 – amar 2013-03-05 13:16:26

+1

我不能相信你所展示的是,你的代碼是正確的,嘗試清理和再次構建。 – 2013-03-05 13:20:56

回答

10

您可能正在運行一個循環內的連接方法。

我想這就是你想要的。

NSMutableArray * array1 = [NSMutableArray array]; // create a Mutable array 

for(id item in items){ 
     [array1 addObject:[item objectForKey:@"id"]]; // Add the values to this created mutable array 
} 

NSString *joinedString = [array1 componentsJoinedByString:@","]; 

NSLog(@"joinedString is %@", joinedString); 
7

你可以做到這一點的,

就拿

NSArray *[email protected][@"A",@"B",@"C"]; 
NSString *string=[array componentsJoinedByString:@","]; 
NSLog(@"%@",string); 

輸出是:

A,B,C 
0

編輯:

首先檢查[item objectForKey:@"id"]是正確與否?

,然後使用下面的代碼:

NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:@"id"], nil]; 
NSString *commaSpStr; 

commaSpStr = [array1 componentsJoinedByString:@", "]; 

NSLog(@"%@", commaSpStr); 
+0

應該'commaSpStr = [listOfSelectedItem componentsJoinedByString:@「,」]; NSLog(@「%@」,commaSpStr);'這不在'for循環之外? 'for loop'將對象添加到數組中,然後使用函數'componentsJoinedByString:'將它們的對象連接到一個字符串中:''''''''''''''''''''''''''''''''''請注意,我沒有倒下,因爲這可以很容易地糾正。 – Popeye 2013-03-05 13:28:57

+0

@ Popeye--首先檢查我的答案,我檢查了它,這是很好:( – iPatel 2013-03-05 13:33:08

+1

@iPatel你是對的,必須有一些功能,所以你不能downvote,而不會在評論中提出理由。@ downvoter認爲。 – Dilip 2013-03-05 13:35:37

1

什麼都你正在寫在[item objectForKey:@"id"]一個正確,也可能是問題一經查實這一個以外的所有的罰款。

NSMutableArray *array = [[NSMutableArray alloc] 
          initWithObjects:@"55",@"56",@"57",@"58", nil]; 

    NSString *joinedString = [array componentsJoinedByString:@","]; 
     NSLog(@"%@",joinedString); 
0

重建array1每次。創建array1的實例變量,將值插入[item objectForKey:@"id"],您將看到joinedString將被更新。

1

我一直在評論這裏的幾個答案,並發現大多數答案只是給出的代碼作爲解決此代碼的答案,原因是因爲代碼提供了(請參閱提供的代碼)完美無瑕。

(通過提問題的人提供)

NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:@"id"], nil]; 
NSString *joinedString = [array1 componentsJoinedByString:@","]; 

NSLog(@"joinedString is %@", joinedString); 

由於用戶沒有提供如何itemNSDictionary創建我假設一個NSArray已創建其中包含一些NSDictionaries

NSArray *array = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"55", @"id", nil], 
        [NSDictionary dictionaryWithObjectsAndKeys:@"65", @"id", nil], 
        [NSDictionary dictionaryWithObjectsAndKeys:@"75", @"id", nil], 
        [NSDictionary dictionaryWithObjectsAndKeys:@"65", @"id", nil], 
        nil]; 

問題是沒有提供的代碼,因爲我們知道itemNSDictionary我們知道[item objectForKey:@"id"]不會返回個別項目它返回ids的NSArray。所以根據它是否爲NSArray它會記錄類似joinedString is (55, 56, 57...)"。我們也知道,它不能只是一個字符串,因爲我們也只有一個值,所以它會記錄下這樣的事情,並且這並不是我們想要的。讓已經提供什麼的唯一方法是有這樣的事情

for(NSDictionary *item in array) { 

    NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:@"id"], nil]; 
    NSString *joinedString = [array1 componentsJoinedByString:@","]; 

    NSLog(@"joinedString is %@", joinedString); 
} 

因此,如果這是不是辦法的情況下解決這個是做

NSMutableArray *array1 = [NSMutableArray array]; 
    for(NSDictionary *item in array) { 

    [array1 addObject:[item objectForKey:@"id"]]; 

    } 
    // Note that this doesn't need to be in a for loop `componentsJoinedByString:` only needs to run once. 
    NSString *joinedString = [array1 componentsJoinedByString:@","]; 
    NSLog(@"joinedString is %@", joinedString); 

這樣做的輸出會(如用戶想)

joinedString is 55,65,75,65 

一旦問題提問者提供缺少的代碼,我會糾正他的回答基於存在的代碼,但在那之前我假設。

0
NSMutableArray *arr = [[NSMutableArray alloc] init]; 

for (NSDictionary *item in array) { 
    [arr addObject:[item objectForKey:@"id"]]; 
} 

NSString *joinedStr = [arr componentsJoinedByString:@","];