2

我正在使用以下代码尝试将数组输出加入到 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
4

7 回答 7

10

您可能正在循环内运行 join 方法。

我想这就是你想要的。

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);
于 2013-03-05T13:21:50.307 回答
7

你可以这样做,

举个例子

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

输出是:

A,B,C
于 2013-03-05T13:17:47.770 回答
1

无论你写的是什么,一个正确的可能是问题,[item objectForKey:@"id"]一旦检查这个另一个都很好。

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

    NSString *joinedString = [array componentsJoinedByString:@","];
        NSLog(@"%@",joinedString);
于 2013-03-05T13:24:06.747 回答
1

我一直在这里评论几个答案,发现大多数答案只是给出提供的代码作为解决此代码的答案,原因是因为提供的代码(请参阅提供的代码)工作得很好.

(由提问者提供)

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

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

由于用户没有提供如何item NSDictionary创建我假设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];

问题在于尚未提供的代码,因为我们知道这item是一个NSDictionary我们知道[item objectForKey:@"id"]不返回单个项目它返回一个NSArrayID 的项目。因此,根据它是否是一个NSArray它会记录类似joinedString is (55, 56, 57...)". 我们也知道它不能只是一个字符串,因为我们也只有一个值,所以它会记录一些这样的事情,这joinedString is 55,又不是我们想要的。获得所提供的东西的唯一方法是拥有这样的东西

 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

一旦提问者提供了缺失的代码,我将根据那里的代码更正他的答案,但在那之前我假设。

于 2013-03-05T14:29:42.887 回答
0

编辑:

首先检查[item objectForKey:@"id"]它是否正确?

然后使用以下代码:

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

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

NSLog(@"%@", commaSpStr);
于 2013-03-05T13:21:47.840 回答
0

你每次都在重新创造。 array1创建一个实例变量array1,向其中插入[item objectForKey:@"id"]值,您将看到joinedString将被更新。

于 2013-03-05T13:58:52.030 回答
0
NSMutableArray *arr = [[NSMutableArray alloc] init];

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

NSString *joinedStr = [arr componentsJoinedByString:@","];
于 2014-04-08T07:23:19.900 回答