NSLocalizedString - Use an undeclared identifier

I use NSLocalizedString in a specific message that works fine if the second parameter is passed as a variable, but not with Use of undeclared identifier 'NSLocalizedString' and Too many arguments provided to function-like macro invocation if the second parameter is written exactly the same as set variable. I have working code using a variable, and this is normal, I just want to understand the reasons for the inability to find out how to avoid this in other situations.

With the following announcements:

 NSString *branchTitle = [branchDictionary objectForKey:@"Title"]; NSString *localString = [NSMutableString stringWithFormat:@"%@ node title", branchTitle]; 

This works fine without errors:

 [navItem setTitle:NSLocalizedString(branchTitle, localString)]; 

... but that seems identical to me, not with the errors noted above:

 [navItem setTitle:NSLocalizedString(branchTitle, [NSMutableString stringWithFormat:@"%@ node title", branchTitle])]; 

Search here and elsewhere I did not find an explanation. I read several hits for each error message and various NSLocalizedString issues, but didn't NSLocalizedString them together. What I found regarding the second error message is probably due to the clang problem and the number of commas in the instruction, which indicates that although the extra comma is in the NSMutableString message, it is still considered as an additional NSLocalizedString parameter. It makes sense?

It doesnโ€™t matter for the question, but the instruction is intended to install a localized version of the title of the navigation bar based on the English version pulled from the dictionary, which differs for different types. The NSMutableString part defines a comment for localization based on the English name.

EDIT: after solving this problem in accordance with the accepted answer below, I noticed another related problem. The localString generated a "Unused variable" warning for the compiler, although it was explicitly used. This is also due to the fact that it is inside the C-macro and for completeness, I add a link to the corresponding entry here to suppress this warning: How can I get rid of the "unused variable" in Xcode

+7
source share
1 answer

I believe this is the result of a poor expansion of C-macro. Indeed, if you write:

 NSLocalizedString(branchTitle, ([NSString stringWithFormat:@"%@ node title", branchTitle])); 

this will compile. For some reason, the preprocessor does not properly process the text inside [] (perhaps because it does not know about Objective-C calls) and treats all the elements in [] as separate arguments:

 #define NSLocalizedString(key, comment) \ [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] 

PS: When I wrote the bad C-macro extension, I meant this :

Macro expansion is a complex operation fraught with unpleasant angular cases and situations that do what, in your opinion, was a great way to optimize the preprocessor extension algorithm incorrectly in a rather subtle way.

+14
source

All Articles