-6

How can I convert NSMutableArray to NSString

bookArray = [[NSMutableArray alloc] init];
[bookArray addObject:@"Book A"];
[bookArray addObject:@"Book B"];
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
kantawit
  • 101
  • 1
  • 1
  • 6
  • 2
    `[bookArray description]` – Hot Licks Jan 26 '13 at 04:16
  • What do you want the result to look like? "Book ABook B"? "Book A, Book B"? "Book A and then Book B"? "(Book A, Book B)"? "[Book A, Book B]"? "'Book A', 'Book B'"? – Kurt Revis Jan 26 '13 at 04:18
  • 1
    possible duplicate of [Join an Array in Objective-C](http://stackoverflow.com/questions/845622/join-an-array-in-objective-c) – Kurt Revis Jan 26 '13 at 04:23
  • You could try [the documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/uid/20000137-BABDECEE). – Caleb Jan 26 '13 at 04:37

2 Answers2

0

As per Hot Licks comment you can use below code.

bookArray = [[NSMutableArray alloc] init];
[bookArray addObject:@"Book A"];
[bookArray addObject:@"Book B"];

Then use Below code.

NSString *yourString=[bookArray description];

if you want to use NSMutableString and Some different separator then use Below Code.

NSMutableString* content = [NSMutableString string];

for (int aa=0; aa < [bookArray count]; aa++){
    [content appendString:[NSString stringWithFormat:@"%@<separator>",[bookArray objectAtIndex:aa]]];
}
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
0

Its very simple :

NSString *string=[bookArray componentsJoinedByString:@", "]; //your separator anything
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140