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 に答える