67

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.

Antoine
  • 21,544
  • 11
  • 81
  • 91
Jhorra
  • 5,873
  • 20
  • 65
  • 113

4 Answers4

171

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,207
  • 7
  • 35
  • 68
rdelmar
  • 102,832
  • 11
  • 203
  • 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
  • 1
    If 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:@""]` 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! – Jack Lawrence Apr 26 '12 at 03:45
  • 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
  • 21,544
  • 11
  • 81
  • 91
  • 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
  • 43,711
  • 13
  • 85
  • 120
2

Swift

var commaSeparatedString = arrayOfEntities.joinWithSeparator(",")
aturan23
  • 2,494
  • 3
  • 16
  • 37
ioopl
  • 1,627
  • 16
  • 18