0

I am trying to convert (or copy?) a NSMutableArray into a NSString. I guess my problem is that I don't really understand the structure of a NSString. In my limited knowledge, a string could look like this: in iphone

2

2 Answers 2

59

Try this code:-

 NSString *string = [array componentsJoinedByString:@","];
0
3

take the NSMutableString and append every array string into your string

like

string = [string appendString:[NSString stringWithFormat:"%@", [array objectAtIndex:i]]];
5
  • 22
    it is quite interesting that the worse answer got accepted. Dear Future Reader: Please check Leena's answer. Jan 10, 2012 at 13:38
  • oh, and actually that code is wrong, -appendString: works in-place and there-for does not return anything, while NSStrings's -stringByAppendingString: returns a new string. Jan 10, 2012 at 15:56
  • 3
    the signature is - (void)appendString:(NSString *)aString, so it is NOT returning a string. Jan 11, 2012 at 12:42
  • and you changed the code in-between, but you are calling the immutable NSString's method -stringByAppendingString:, so your answer is still a mess, as you are referring to NSMutableString. But even if you fix that, your answer is still horribly inefficient. Jan 11, 2012 at 12:45
  • Here your code is not even compiling Semantic Issue: Assigning to 'NSMutableString *' from incompatible type 'void', and you included a new bug: stringWithFormat: needs a objective-c string: @"%@" Jan 11, 2012 at 12:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.