Concatenating NSArray contents with NSMutableString AppendString
-
19-08-2019 - |
Question
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
Solution
you have an unclosed bracket
you need ]]
at the end instead of ]
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow