ios - 附加到 NSString 的推荐方法是什么

标签 ios iphone cocoa-touch nsstring nsmutablestring

我正在执行一些字符串连接以创建数据库查询。 作为其中的一部分,我需要分配和重新分配 NSString 变量以便附加到它。

我目前正在使用这段代码:

NSString *retVal = [[NSString alloc]init];
NSString *concat = @"";
retVal = [NSString stringWithFormat:@"%@%@ myfield= 'myvalue'", retVal, concat];

请注意,每当 retVal 和 concat 保留“”(空字符串)时,我都会在 retVal 中返回空字符串。这绝对不是预期的,因为我应该得到“myfield='myvalue'”。

我错过了什么?

更新:

这是我最后一次尝试:

NSMutableString * retVal =  [[NSMutableString alloc] init]; 
NSString * concat = @"";

[retVal appendString:@"appendstring"];


NSLog(@"%@", retVal); // prints <object returned empty description>  

[retVal appendString:concat];
[retVal appendString:@"appendstring1"];

NSLog(@"%@", retVal);  // prints %@

最佳答案

    NSString *retVal = @"";
    NSString *concat = @"";
    //do whatever stuff you want with retVal and concat 
    //once finished then do this below
    if(retVal.length==0||concat.length==0)
    {
    retVal = @"myfield= 'myvalue'";
    }
    else
    retVal = [NSString stringWithFormat:@"%@%@ myfield= 'myvalue'", retVal, concat];

关于ios - 附加到 NSString 的推荐方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13791889/

相关文章:

ios - 如何在下拉屏幕时关闭通知中心的显示?

ios - 加载请求的应用程序时出现问题。看起来您可能正在使用 LAN URL

iphone - Storyboard - 在 Storyboard 中为同一个 ViewController 创建两个不同的 View

ios - 如何在 AdMob 中添加中介网络?

ios - UIActivityIndi​​catorView 直到加载完成后才显示

ios - 如果数据类型为 URL 的变量为空,如何在 Swift 中检查?

objective-c - 升级到 iOS 6 后 UISegmentedControl 的显示问题

iphone - 未找到 SoundCloud 库

cocoa-touch - iPad 上 PDF 的替代方案

ios - 为什么在旋转模态呈现的 subview Controller 时我的约束不起作用?