So I have a nsmutablearray with a bunch of objects in it. I want to create a comma separated string of the id value of each object.
Asked
Active
Viewed 4.0k times
4 Answers
172
Use the NSArray
instance method componentsJoinedByString:
.
In Objective-C:
- (NSString *)componentsJoinedByString:(NSString *)separator
In Swift:
func componentsJoinedByString(separator: String) -> String
Example:
In Objective-C:
NSString *joinedComponents = [array componentsJoinedByString:@","];
In Swift:
let joinedComponents = array.joined(seperator: ",")

Lukas Würzburger
- 6,543
- 7
- 41
- 75

rdelmar
- 103,982
- 12
- 207
- 218
-
So the objects in my array have 4 or 5 properties, how do I tell it to join just the id values? – Jhorra Apr 26 '12 at 03:36
-
1If you just log one of your objects what do you get? Just the id values? If so, that's what you'll get with componentsJoinedBuString: Try it and see. – rdelmar Apr 26 '12 at 03:39
-
13@Jhorra If you want an array of just one of the properties, you can do something really awesome: `[
valueForKey:@" – Jack Lawrence Apr 26 '12 at 03:45"]` and it'll return an array of just that property. Then call `componentsJoinedByString:` on the result. Behind the scenes, the framework is iterating through all of the objects in the array and calling `valueForKey:` on them. Key-Value Coding is awesome! -
For more KVC Collection Operators, check this out: http://nshipster.com/kvc-collection-operators/ – Jay Q. Feb 09 '15 at 23:11
7
If you're searching for the same solution in Swift, you can use this:
var array:Array<String> = ["string1", "string2", "string3"]
var commaSeperatedString = ", ".join(array) // Results in string1, string2, string3
To make sure your array doesn't contains nil values, you can use a filter:
array = array.filter { (stringValue) -> Bool in
return stringValue != nil && stringValue != ""
}

Antoine
- 23,526
- 11
- 88
- 94
-
This doesn't seem to work for NSMutableArray in Swift. Searching Google has yet to reveal an answer. – ultrageek Mar 08 '15 at 22:52
-
@Twan Thank you, your answer is (almost) exactly what I was looking for... Almost, because I need to concatenate optional Strings, some of which could be nil (and so I don't want to concatenate them), but this solution doesn't accept optionals String, it requires to unwrap them... maybe you had a similar issue in the past, if so could you help me? – cdf1982 Apr 12 '15 at 10:31
-
@cdf1982 I've updated my answer. You can do this using the filter method. – Antoine Apr 12 '15 at 15:07
-
@Twan it's perfect, thank you really, really, really very much! I'm just sorry that this is a comment and I can't accept it! – cdf1982 Apr 12 '15 at 15:22
5
Create String from Array:
-(NSString *)convertToCommaSeparatedFromArray:(NSArray*)array{
return [array componentsJoinedByString:@","];
}
Create Array from String:
-(NSArray *)convertToArrayFromCommaSeparated:(NSString*)string{
return [string componentsSeparatedByString:@","];
}

meda
- 45,103
- 14
- 92
- 122
2
Swift
var commaSeparatedString = arrayOfEntities.joinWithSeparator(",")
-
1The question specified a programming language. Please consider changing it to objective - c. – handiansom Mar 16 '17 at 05:40
-
-