开发者

Pull all objects in NSMutableArray and spit it out into a NSString

开发者 https://www.devze.com 2023-02-19 14:06 出处:网络
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

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 开发者_Go百科
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


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号