ios - NSString concat在设备上不起作用

标签 ios objective-c nsstring concatenation

我有一个小挑战。我正在使用以下代码来连接一个NSString。在控制台上,输出正常,在设备上不起作用。我似乎无法弄清楚我所缺少的。

- (void)loadContent {
    getHTML = @"leap";
    NSLog(@"getHTML start: %@", getHTML);
    int showLeap = selectedLeap +1;
    NSString *leapString = [NSString stringWithFormat:@"%d", showLeap];

    if (showLeap<10) {
        getHTML = [getHTML stringByAppendingString:@"0"];
    }

    getHTML = [getHTML stringByAppendingString:leapString];
    getHTML = [getHTML stringByAppendingString:selectedMenu];
    NSLog(@"getHTML complete: %@", getHTML);

在模拟器上,输出为:
2015-06-03 12:11:57.619 iOS_TWW_EVOLUTION[917:582733] getHTML start: leap
2015-06-03 12:11:57.621 iOS_TWW_EVOLUTION[917:582733] getHTML complete: leap01_summary

在设备上,输出为:
2015-06-03 12:15:40.341 iOS_TWW_EVOLUTION[917:582733] getHTML start: leap
2015-06-03 12:15:40.346 iOS_TWW_EVOLUTION[917:582733] getHTML complete: getHTML complete: 

最佳答案

我找到了解决方案。 [NSString stringByAppendingString ...]方法不断在设备上返回nil有点奇怪。这是我使用[NSString stringWithFormat ...]方法的解决方案;

int showLeap = selectedLeap +1;
NSString *first = @"leap";
NSString *second = @""; if (showLeap<10) { second=@"0";}
NSString *third = [NSString stringWithFormat:@"%d", showLeap];
NSString *fourth = selectedMenu;
NSString *getHTML = [NSString stringWithFormat: @"%@%@%@%@", first, second, third, fourth];
NSLog(@"getHTML complete: %@", getHTML);

关于ios - NSString concat在设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618612/

相关文章:

ios - Crashlytics 下载文件

objective-c - 为什么我的图表如此不稳定?

objective-c - 简单的写入文件对我来说不起作用

ios - 从 NSString 返回数字

objective-c - 调试 iOS 应用程序时如何在 Xcode 控制台中打印 unsigned char?

iphone - 如何在 iPhone 中将 nsstring 更改为 nsdate 格式?

iOS API - NSURLConnectionDelegate 与 NSURLConnectionDataDelegate 之间的不同

ios - 使用未声明的类型 UNAuthorizationOptions

ios - 从 AppDelegate 到 PresentedViewController 的警报 : "Attempt to present UIAlertController on ... whitch is already presenting UIAlertController"

ios - 使用 iPhone 作为带蓝牙的键盘楔