hanumanDev
hanumanDev

Reputation: 6614

How to join an NSArray output to an NSString separated with commas

I'm using the following code to try to join the array output into an NSString.

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

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

I would like this to output the joined string as: joined string is 55,56,57,66,88... etc... at the moment the output is:

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

Upvotes: 2

Views: 3773

Answers (7)

user1215486
user1215486

Reputation:

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

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

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

Upvotes: 0

Popeye
Popeye

Reputation: 12123

I have been commenting on a couple of the answers here and found that most of the answers are just giving the code provided as the answer to solve this code, and the reason for that is because the code provided (See Provided code) works perfectly fine.

(Provide by question asker)

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

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

As the user hasn't provided how the item NSDictionary is created I am assuming that an NSArray has been created which contains some 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];

The problem is with the code that hasn't been provide, because we know that item is an NSDictionary we know that [item objectForKey:@"id"] doesn't return an individual items it returns an NSArray of ids. So based on if it was an NSArray it would log something like joinedString is (55, 56, 57...)". We also know that it can't just be a string as we would also only have one value than so it would log some thing like this joinedString is 55, and again this isn't what is wanted so. the only way to get what has been provided would be to have something like this

 for(NSDictionary *item in array) {

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

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

So if this is the case than the way to resolve this would be to do

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

The output of this would be (As user wants)

  joinedString is 55,65,75,65

Once the question asker provides the missing code I will correct his to answer based on there code but until then I am assuming.

Upvotes: 1

Samet DEDE
Samet DEDE

Reputation: 1621

You are recreating array1 everytime. Create an instance variable of array1, insert [item objectForKey:@"id"] value to it and you will see joinedString will be updated.

Upvotes: 0

iPatel
iPatel

Reputation: 47119

EDIT:

First Check [item objectForKey:@"id"] it is proper or not ??

And Then use following code :

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

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

NSLog(@"%@", commaSpStr);

Upvotes: 0

Balu
Balu

Reputation: 8460

What ever you are writing that one correct may be problem in [item objectForKey:@"id"] once check this one other all are fine.

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

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

Upvotes: 1

Praveen S
Praveen S

Reputation: 10393

You are probably running the join method inside a loop.

I suppose this is what you want.

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

Upvotes: 10

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

You can do it as,

take for example

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

Output is :

A,B,C

Upvotes: 7

Related Questions