1

我想用一组对象构造一个字符串

for (int n=0; n<[friends count]; n++) {
    User* friend = [friends objectAtIndex:n];
    if (n>=[friends count]-1) {
        NSString* param = [NSString stringWithFormat:@"{\"target_id\":\"dummy(%d)\"}", [friend uid]];
        NSString* sf2 = [[NSString alloc]init];
        sf2= [sf stringByAppendingString:param];
        [sf release];
        sf = [[NSString alloc]initWithString:sf2];
        [sf2 release];
        [param release];
    }else{
        NSString* param = [NSString stringWithFormat:@"{\"target_id\":\"dummy(%d)\"},", [friend uid]];

        NSString* sf2 = [[NSString alloc]init];
        sf2= [sf stringByAppendingString:param];
        [sf release];
        sf = [[NSString alloc]initWithString:sf2];
        [sf2 release];
        [param release];
    }

}

我想知道我做错了什么。提前致谢!!

4

2 回答 2

1

param 是自动释放的对象,所以你不应该释放它。

编辑://在此处初始化 sf。

for (int n=0; n<[friends count]; n++) {
    User* friend = [friends objectAtIndex:n];
    if (n>=[friends count]-1) {
        NSString* param = [NSString stringWithFormat:@"{\"target_id\":\"dummy(%d)\"}", [friend uid]];
        sf = [sf stringByAppendingString:param];
    }else{
        NSString* param = [NSString stringWithFormat:@"{\"target_id\":\"dummy(%d)\"},", [friend uid]];

        sf = [sf stringByAppendingString:param];
    }

}
于 2012-07-03T15:04:42.107 回答
0

使用此代码.... 注释 [sf release] 和 [param release];

for (int n=0; n<[friends count]; n++) {
User* friend = [friends objectAtIndex:n];
if (n>=[friends count]-1) {
    NSString* param = [NSString stringWithFormat:@"{\"target_id\":\"dummy(%d)\"}", [friend uid]];
    NSString* sf2 = [[NSString alloc]init];
    sf2= [sf stringByAppendingString:param];
    //[sf release];
    sf = [[NSString alloc]initWithString:sf2];
    [sf2 release];
    //[param release];
}else{
    NSString* param = [NSString stringWithFormat:@"{\"target_id\":\"dummy(%d)\"},", [friend uid]];

    NSString* sf2 = [[NSString alloc]init];
    sf2= [sf stringByAppendingString:param];
    //[sf release];
    sf = [[NSString alloc]initWithString:sf2];
    [sf2 release];
   //[param release];
}

}

这可能会帮助你

于 2012-07-03T15:04:32.500 回答