Objective-C 连接方法的差异

发布于 2024-11-18 05:55:28 字数 1071 浏览 2 评论 0原文

这里澄清问题。 我最初尝试使用 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 是否不像我想象的那样工作?

谢谢!

Clarification question here.
I was originally attempting to concatenate two strings using the stringByAppendingString method:

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);

However, the NSLog was just outputing "," as if cycleCountString was not being appended to dataString.

After reading:http://stackoverflow.com/questions/510269/how-do-i-concatenate-strings-in-objective-c , I was able to fix the issue by instead doing a 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);

any idea why the previous method wasn't working? Does stringByAppendingString not work the way I think it does?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城旧梦 2024-11-25 05:55:28

您永远不会更新 dataString 的值。

dataString = [dataString stringByAppendingString: cycleCountString];

You're never updating the value of dataString.

dataString = [dataString stringByAppendingString: cycleCountString];
救赎№ 2024-11-25 05:55:28

NSString 实例是不可变的 - 您无法更改它们。 stringByAppendingString: 不会更改原始 NSString 实例,它会创建一个新的 NSString 并返回它。

新版本之所以有效,是因为您将 stringWithFormat: 的结果分配给 dataString。你可以让旧版本使用这样的东西:

dataString = [dataString stringByAppendingString:cycleCountString];

NSString instances are immutable - you can't change them. stringByAppendingString: doesn't change your original NSString instance, it creates a new NSString and returns that.

The new version works because you assign the result of stringWithFormat: to dataString. You could make the old version work with something like this:

dataString = [dataString stringByAppendingString:cycleCountString];
滥情空心 2024-11-25 05:55:28

文档中的示例

NSString *errorTag = @"Error: ";
NSString *errorString = @"premature end of file.";
NSString *errorMessage = [errorTag stringByAppendingString:errorString];

生成字符串“错误:文件过早结束。”。

Example in documentation

NSString *errorTag = @"Error: ";
NSString *errorString = @"premature end of file.";
NSString *errorMessage = [errorTag stringByAppendingString:errorString];

produces the string “Error: premature end of file.”.

手心的海 2024-11-25 05:55:28

因为你没有将agian分配给数据字符串,

NSString *dataString = @",";
NSInteger i = 0;
NSString *cycleCountString = @"";
for (i = 0; i<[[self cycleList] count]; i++) {

    cycleCountString = [NSString stringWithFormat:@"cycle#%d,",i];

    dataString=[dataString stringByAppendingString:cycleCountString];//this Line
}

NSLog(@"DataString is: %@",dataString);

数据字符串是:,cycle#0,cycle#1,cycle#2,

because u didn't assign agian to datastring

NSString *dataString = @",";
NSInteger i = 0;
NSString *cycleCountString = @"";
for (i = 0; i<[[self cycleList] count]; i++) {

    cycleCountString = [NSString stringWithFormat:@"cycle#%d,",i];

    dataString=[dataString stringByAppendingString:cycleCountString];//this Line
}

NSLog(@"DataString is: %@",dataString);

DataString is: ,cycle#0,cycle#1,cycle#2,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文