使用NSMutableString AppendString连接NSArray内容

Dav*_*ave 1 iphone objective-c string-concatenation

我正在尝试迭代NSArray并在我尝试将位置i的数组内容连接到我的NSMutableString实例时,继续获得编译器错误.

它只是告诉我之前有"语法错误"; 这并没有告诉我很多.在这一行:

  [output appendString:[widget.children objectAtIndex:i]; 
Run Code Online (Sandbox Code Playgroud)

我知道我的语法必须有一些东西..

我的功能如下

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

}
Run Code Online (Sandbox Code Playgroud)

提前致谢

Ash*_*ark 13

NSArray 有一个方法可以完成您正在做的事情:

- (NSString *)readArray {
    return [widget.children componentsJoinedByString:@""];
}
Run Code Online (Sandbox Code Playgroud)

此外,除非你在紧密循环中相当频繁地调用该函数,否则创建它自己的自动释放池没有多大优势.