澄清问题在这里。我最初试图使用以下stringByAppendingString
方法连接两个字符串:
NSString *dataString = @",";
NSInteger i = 0;
NSString *cycleCountString = @"";
for (i = 0; i<[[self cycleList] count]; i++) {
cycleCountString = [NSString stringWithFormat:@"cycle#%d,",i];
[dataString stringByAppendingString:cycleCountString];
}
NSLog(@"DataString is: %@",dataString);
但是,NSLog 只是输出“ ,
”,就好像cycleCountString
没有附加到dataString
.
阅读后:http://stackoverflow.com/questions/510269/how-do-i-concatenate-strings-in-objective-c ,我能够通过执行以下操作来解决问题stringWithFromat
:
NSString *dataString = @",";
NSInteger i = 0;
NSString *cycleCountString = @"";
for (i = 0; i<[[self cycleList] count]; i++) {
cycleCountString = [NSString stringWithFormat:@"cycle#%d,",i];
dataString = [NSString stringWithFormat:@"%@%@",dataString,cycleCountString];
}
NSLog(@"DataString is: %@",dataString);
知道为什么以前的方法不起作用吗?stringByAppendingString
不像我想的那样工作?
谢谢!