1

Hi i have a nsmutable array full of keywords and i wish to key each object and spit it out with a comma after each object example below. The NSMutable array is called KeywordArray

Array Structure

Keyword 1
Keyword 2
Keyword 3
Keyword 4 
Keyword 5
Keyword 6 
Keyword 7

I wish to convert that NSMutableArray into the following format within a NSString

Keyword 1, Keyword 2, Keyword 3, Keyword 4, Keyword 5, Keyword 6, Keyword 7

Thanks

Mason

3 Answers 3

5

You can use the componentsJoinedByString: method of NSArray to join the elements in the array using a separator. This will work with an NSMutableArray as well, because NSMutableArray inherits from NSArray.

NSMutableArray *array = ...;
NSString *string = [array componentsJoinedByString:@", "];

See the NSArray class reference for more information.

Sign up to request clarification or add additional context in comments.

Comments

3

You can do it easily:

NSMutableArray *testArray = [[NSMutableArray alloc] initWithObjects:@"keyword1", @"keyword2" @"keyword3", nil];
NSString *string = [testArray componentsJoinedByString:@","];

Same case, but with NSArray was discussed here

Comments

2

NSArray *arr;
[arr componentsJoinedByString:@", "];

2 Comments

Doesn't matter, componentsJoinedByString: works for NSMutableArray and NSArray. Returns a NSString.
It doesn't matter that it's a mutable array, because NSMutableArray is a subclass of NSArray. Therefore it has access to all methods that NSArray has.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.