3

I am performing some string concatenation to create a database query. As part of this, I need to assign and re-assign NSString variable in order to append to it.

I am currently using this code:

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

Note that whenever retVal and concat hold "" (empty string), I get empty string back in retVal. This is definitely not expected as I should get " myfield= 'myvalue'".

What am I missing?

Update:

Here is last I tried:

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 %@
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • Why are you alloc-initting `retVal` first and the reassign to it? –  Dec 09 '12 at 21:16
  • I copied and ran your code and it worked fine for me - where are you running your code? Is it possible that something else is setting retVal to an empty string? – redlightbulb Dec 09 '12 at 21:56
  • Worked fine for me too. Where are you logging? I'm guessing that retVal has been deallocated by the time you check it. – rdelmar Dec 09 '12 at 22:47
  • I tried number of approaches and it runs weirdly, eg the nsmutablestring approach listed below. I don't even remember how much I already tried, but it acts weirdly. This iOS 5.1 simulator, xcode 4.5. – Nirav Bhatt Dec 10 '12 at 06:40

2 Answers2

2
    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];
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
1

Either use an NSMutableString with appendString:, use stringByAppendingString:, or construct the full string with a single stringWithFormat:, plugging in the values you need all at once.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • The question is _Note that whenever retVal and concat hold "" (empty string), I get empty string back in retVal. This is definitely not expected as I should get " myfield= 'myvalue'"._ OP already knows how to append strings. – iDev Dec 09 '12 at 21:37
  • This code gives me empty string in log: NSMutableString * retVal = [[NSMutableString alloc] init];[retVal appendString:@"mystring"]; NSLog(@"%@", retVal); – Nirav Bhatt Dec 10 '12 at 06:38
  • this approach works. The issue was, I was moving instruction pointer within if-else blocks. When I ran code straightforward, it worked. Moving of pointer was definitely not related to any strings I was messing with, yet it affected the strings being concatenated. Possible a bug in LLDB. – Nirav Bhatt Dec 10 '12 at 14:02