22

Possible Duplicate:
How do I concatenate strings in Objective-C?

Firstly, the platform is iPhone and label.text changes the label displayed. Consider this scenario:

I've an array of integers. And I want to display it on the screen.

Here's my take on it:

-(IBAction) updateText: (id)sender {
   int a[2];
   a[0]=1;
   a[1]=2;
   a[2]=3;
   for (int i=0; i<=10;i++)
     label.text = [NSString stringByAppendingString: [NSString stringWithFormat: @"%i", a[i]]]; 
}

As you can probably see, I'm pretty confused. Pls pls help me out :(

Community
  • 1
  • 1
r0ach
  • 511
  • 3
  • 6
  • 13
  • A bit late but you initialize your array with size 2 so highest index would be 1, but you're doing a[2] = 3; which would cause out of bounds error. – Jonny Jun 10 '11 at 17:32

4 Answers4

36

Try this:

NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
    [theString appendString:[NSString stringWithFormat:@"%i ",i]];
}
label.text = theString;
Tom Dalling
  • 21,742
  • 6
  • 57
  • 78
  • This is possible only in NSMutableString, right? I'm getting confused with all these different classes and their methods... But, thx man. This is just what I need. :) – r0ach Jul 21 '09 at 12:41
  • appendString: is only in NSMutableString, but a similar function is stringByAppendingString: on NSString. – Tom Dalling Jul 21 '09 at 22:36
  • 1
    @TomDalling : I have used same approach but 'theString' is declared in .h file since the delegate within which append is to performed is invoked multiple times. Now, in this case I'm getting an empty 'theString'. Any fix? – Jayprakash Dubey Oct 15 '14 at 10:05
17

Since you're using a loop, do be somewhat careful with both Tom and Benjie's solutions. They each create an extra autoreleased object per iteration. For a small loop, that's fine, but if the size of the loop is unbounded or if the strings are large, this can lead to a very large memory spike and performance hit. Particularly on iPhone, this is exactly the kind of loop that can lead to surprising memory problems due to short-lived memory spikes.

The following solution has a smaller memory footprint (it's also slightly faster and takes less typing). Note the call to -appendFormat: rather than -appendString. This avoids creating a second string that will be thrown away. Remember that the final string has an extra space at the end that you may want to get rid of. You can fix that by either treating the first or last iteration differently, or by trimming the last space after the loop.

NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
    [theString appendFormat:@"%i ",i];
}
label.text = theString;

Don't forget [NSArray componentsJoinedByString:]. In this case you don't have an NSArray, but in the common cases where you do, this is probably the best way to get what you're looking for.

Rob Napier
  • 250,948
  • 34
  • 393
  • 528
7
//NSArray *chunks   
string = [chunks componentsJoinedByString: @","];
Hiren
  • 12,525
  • 7
  • 51
  • 71
Kit
  • 71
  • 1
  • 1
4

Another method without using NSMutableString:

NSString* theString = @"";
for (int i=0; i<=10;i++){
    theString = [theString stringByAppendingFormat:@"%i ",i];
}
label.text = theString;

Here's a full implementation (correcting your ranges):

-(IBAction) updateText: (id)sender {
     int a[3];
     a[0]=1;
     a[1]=2;
     a[2]=3;
     NSString *str = @"";
     for (int i=0; i<3;i++)
       str = [str stringByAppendingFormat:@"%i ",i];
     label.text = str;
}

You could also do it like this (e.g. if you wanted a comma separated list):

-(IBAction) updateText: (id)sender {
     int a[3];
     a[0]=1;
     a[1]=2;
     a[2]=3;
     NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];
     for (int i=0; i<3;i++)
         [arr addObject:[NSString stringWithFormat:@"%i",i]];

     label.text = [arr componentsJoinedByString:@", "];
}
Benjie
  • 7,063
  • 4
  • 24
  • 43
  • 1
    While the LISP programmer in me prefers the NSString approach over NSMutableString, you really have to be careful with the memory impact of your first solution. For such a small loop, it's not a big deal, but this is O(n^2) in memory and can cause problems on iPhone where even a short-lived memory spike is an issue. – Rob Napier Jul 21 '09 at 14:10
  • Very true. The array method is much better for this reason. (Would be better still if I had done [NSMutableArray alloc] and [arr release]...) It also gives added flexibility in the joining string without having to worry about missing the comma or space off the beginning or end of the list. – Benjie Jul 22 '09 at 13:42