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:@", "];
精彩评论