Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert elements of NSMutableArray to NSString

Tags:

cocoa

iphone

I have 1 NSMutableArray and I want to convert whatever data in array will be in NSString. tell me code for that. Array is nothing but object of NSMutableArray class.

like image 604
user174761 Avatar asked Dec 30 '25 07:12

user174761


2 Answers

If you want only the elements of array then you can try componentsJoinedByString:. This method returns all the elements with separator string without other formatting info.

[array componentsJoinedByString:@","];

Here "," is separator string.

like image 63
Jami Avatar answered Jan 02 '26 03:01

Jami


It depends on how you want your string. One approach could be iterate through array and convert each element of it.

NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
    [result appendString:[obj description]];
}
NSLog(@"The concatenated string is %@", result);

You can modify the above code based on item's class.

Below code will convert Array to string with commas and other information.

NSString * result = [array description];
like image 21
Unicorn Avatar answered Jan 02 '26 05:01

Unicorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!