Search code examples
objective-ciosparametersnslocalizedstring

Error with a NSLocalizedString with parameter


I have to print a localized string passing it a parameter.

I use:

 NSString *myMsg = [[NSString alloc] 
   stringByAppendingFormat:NSLocalizedString(@"MyKey", @""), aString];
 [MyViewController updateMyMessage:myMsg];
 [myMsg release];

In the Localizable.strings, I use: "MyKey" = "My message says: %@";

EDIT:

It works, using this code:

NSString *myMsg = [NSString stringWithFormat:NSLocalizedString(@"MyKey", @""), aString];
 [MyViewController updateMyMessage:myMsg];

However, I would to know what's the problem in the the former code.


Solution

  • Fran,

    stringByAppendingFormat appends to an existing string. Since myMsg has not been initialized the first example doesn't work.

    You can reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html and make sure you are using a method for creating or initializing strings.

    Hope that helps,

    Ryan