Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Simplest way to create comma separated string from an array of objects

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.

like image 785
Jhorra Avatar asked Apr 26 '12 03:04

Jhorra


People also ask

How do you get a comma separated string from an array in C?

How to get a comma separated string from an array in C#? We can get a comma-separated string from an array using String. Join() method. In the same way, we can get a comma-separated string from the integer array.

How do you make a comma separated string from an array?

Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc. If separator is an empty string, the string is converted to an array of characters.

How do you get comma separated values in an array?

Use the String. split() method to convert a comma separated string to an array, e.g. const arr = str. split(',') . The split() method will split the string on each occurrence of a comma and will return an array containing the results.


1 Answers

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: ",") 
like image 130
rdelmar Avatar answered Oct 04 '22 06:10

rdelmar