1

我想以以下格式创建一个字符串:id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&等。在for循环中我得到了id和stringdata,但是当我打印出testString时说(null),但是当我打印出Ident和Stringdata时(第一个两个 NSLogs) id 确实打印出来了,stringdata 也显示为 (null),因为此时字符串是空的,所以这很正常。

for(NSUInteger i = 0; i<self.childViewControllers.count; i++) {

        NSLog(@"Ident: %@",((myViewController*)[self.childViewControllers objectAtIndex:i]).ident);
        NSLog(@"Stringdata: %@",((myViewController*)[self.childViewControllers objectAtIndex:i]).getQAnswer);

        testString = [testString stringByAppendingString:([NSString stringWithFormat: @"id[]=%@&stringdata[]=%@&",((myViewController*)[self.childViewControllers objectAtIndex:i]).ident ,((myViewController*)[self.childViewControllers objectAtIndex:i]).getQAnswer])];

        NSLog(@"PostString : %@",testString);
}
4

3 回答 3

3

这意味着testStringnil你进入这个循环时。因此,每次调用时stringByAppendingString:,您都将其传递给一个nil对象,该对象什么都不做。在循环结束时,testString仍然是nil.

于 2013-04-04T12:43:39.630 回答
0
NSMutableString *mString = [NSMutableString string];
for(NSUInteger i = 0; i<self.childViewControllers.count; i++)
{
    [mString appendFormat:@"%@id[]=%@",(i>0)?@"&":@"",((myViewController*)[self.childViewControllers objectAtIndex:i]).ident];
    [mString appendFormat:@"&stringdata[]=%@",((myViewController*)[self.childViewControllers objectAtIndex:i]).getQAnswer];
}
NSLog(@"PostString : %@",mString);

希望这可以帮助

使用它输出的测试字符串:

2013-04-04 23:46:54.619 PagingScrollView[93543:c07] PostString : id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]= TESTQ&id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]=TESTQ&id[]=TESTI&stringdata[]=TESTQ

于 2013-04-04T12:44:30.947 回答
0

您正在将该格式附加到testString. 由于它没有被初始化,它nil和任何消息都nil返回nil

你应该使用:

testString = [NSString stringWithFormat: @"id[]=%@&stringdata[]=%@&",((myViewController*)[self.childViewControllers objectAtIndex:i]).ident ,((myViewController*)[self.childViewControllers objectAtIndex:i]).getQAnswer];
于 2013-04-04T12:44:31.337 回答