【问题标题】:Concatenating NSArray contents with NSMutableString AppendString用 NSMutableString AppendString 连接 NSArray 内容
【发布时间】:2009-03-17 04:36:38
【问题描述】:

我试图遍历一个 NSArray 并在我尝试将位置 i 处的数组内容连接到我的 NSMutableString 实例时不断收到编译器错误。

它只是告诉我有一个“之前的语法错误;”这并没有告诉我很多。 在这一行:

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

我知道我的语法一定有问题..

我的功能如下

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

}

提前致谢

【问题讨论】:

    标签: objective-c iphone string-concatenation


    【解决方案1】:

    NSArray 有一个方法可以做你正在做的事情:

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

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

    【讨论】:

      【解决方案2】:

      你有一个未闭合的括号
      最后你需要]] 而不是]

      【讨论】:

        猜你喜欢
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        相关资源
        最近更新 更多