views:

5553

answers:

5

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 :(

+4  A: 

Try this:

NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
    [theString appendString:[NSString stringWithFormat:@"%i ",i]];
}
label.text = theString;
Tom Dalling
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
appendString: is only in NSMutableString, but a similar function is stringByAppendingString: on NSString.
Tom Dalling
+1  A: 

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 Gillam
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
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 Gillam
+2  A: 

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
Thx for the heads up, Rob. :)
r0ach
A: 

//NSArray *chunks
string = [chunks componentsJoinedByString: @","];

Kit