0

I found a few questions like this but I couldn't find this particular question. I have a number of strings that are initialized as

NSString *string = [[NSString alloc] init];

which are then assigned a value depending on the results of an if/else block:

if ([anotherThing isEqualToString:@"boogers"]) {
    string = [NSString stringWithFormat:@"some characters"];
} else {
    string = [NSString stringWithFormat:@"some _other_ characters"];
}

and then string is used later in the method.

How can I accomplish this without leaving a dead store at the alloc/init stage? If I alloc inside the if (or the else), the string is gone by the time I need it several lines down.

ele
  • 6,021
  • 5
  • 25
  • 35
  • Why are you allocing and initing the "dead" string in the first place? Why not set it to `nil` since your execution path guarantees it will be assigned? – vcsjones Nov 29 '12 at 22:09
  • 1
    In addition to Joe Hankin's answer, if your strings don't need to be formatted, you can just use `string = @"some characters";`. – msoler Nov 29 '12 at 22:21
  • Thanks, @vcsjones. I didn't know you could do that. @msoler, is there a good explanation somewhere on the real difference between `stringWithFormat:` and `stringWithString:`? I'm unsure how a formatted string differs from any others. – ele Nov 29 '12 at 22:36
  • `stringWithFormat:` is for creating a string that includes the contents of variables, e.g. `[NSString stringWithFormat:@"There are %d states in %@", anInt, anotherString];` `stringWithString:` is just a copy constructor. – Joe Hankin Nov 29 '12 at 22:42
  • @JoeHankin, thanks. That makes perfect sense. – ele Nov 29 '12 at 22:46
  • Check Apple's official doc about [formatting string objects](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html#//apple_ref/doc/uid/20000943) and [string format specifiers](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265). – msoler Nov 29 '12 at 22:50

2 Answers2

5

You don't have to initialize the string on that first line -- you just need to declare it:

NSString *string = nil;
if ([anotherThing isEqualToString:@"boogers"]) {
    string = @"some characters";
} else {
    string = @"some _other_ characters";
}
Joe Hankin
  • 950
  • 8
  • 15
  • Why are you using stringWithFormat: in your answer when you correctly stated in your comments, that it should be used when you have a format string? – rdelmar Nov 29 '12 at 22:56
  • Fair point -- I was copying Erik's original code, but I've edited my post to reflect your comment. – Joe Hankin Nov 29 '12 at 23:15
0

The [NSString stringWithFormat:] will initialize a new NSString object for you, basically what you are doing is declaring a new object each time, just set the pointers for your strings as NSSTring *someString, *someOtherString, *allTheStringsYouNeed; and then use any class method to initialize it even @"Characters"; will work correctly as the compiler do it at runtime for you.

Sergio Prado
  • 229
  • 2
  • 3