Thursday, April 14, 2011

Concatenating NSArray contents with NSMutableString AppendString

I'm trying to iterate through an NSArray and keep getting a compiler error right when i try concatenating the contents of my array at position i to my NSMutableString instance..

It just tells me that there's a "syntax error before ;" which doesn't tell me a whole lot. at this line:

  [output appendString:[widget.children objectAtIndex:i];

i know there must be something up with my syntax..

my function is as follows

- (NSString *)readArray
{
    NSMutableString *output = [[NSMutableString alloc] init];
    int i;
    int arraySize = widget.children.count;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    for (i = 0; i < arraySize; i++)
    {
     [output appendString:[widget.children objectAtIndex:i];  (throws error here)
    }
    [pool release];
    return output;

}

thanks in advance

From stackoverflow
  • you have an unclosed bracket
    you need ]] at the end instead of ]

    Dave : FRAKK me.. you're right.. doh! thank you!
  • NSArray has a method that does exactly what you're doing as well:

    - (NSString *)readArray {
        return [widget.children componentsJoinedByString:@""];
    }
    

    Also, unless you're calling that function fairly frequently in a tight loop there's not much advantage to having it create its' own autorelease pool.

0 comments:

Post a Comment