我想构造一个包含对象数组的字符串
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];
}
}
我想知道我做错了什么。 在此先感谢!!
答案 0 :(得分: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];
}
}
答案 1 :(得分: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];
}
}
这可能会对你有所帮助