NSLocalizedString - 使用未声明的标识符问题 [英] NSLocalizedString - Use of undeclared identifier issue

查看:577
本文介绍了NSLocalizedString - 使用未声明的标识符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个特定的消息中使用 NSLocalizedString ,如果第二个参数作为一个变量传递,但失败了使用未声明标识符'NSLocalizedString'提供给函数式宏调用的太多参数如果第二个参数的写入与变量的设置完全相同。我有工作代码使用的变量,这是好的,我只是想了解失败的原因,知道如何避免在其他情况下。

I'm using NSLocalizedString in a particular message that works fine if the second parameter is passed as a variable, but fails 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 the variable was set. I have working code using the variable, which is fine, I just want to understand the reasons for the failure to know how to avoid it in other situations.

有以下声明:

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

这样工作正常,没有错误:

This works fine with no errors:

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

...但这似乎与我完全相同, p>

... but this, which seems identical to me, fails with the errors noted above:

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

在这里和其他地方搜索我还没有找到解释。我阅读了每个错误消息和各种 NSLocalizedString 问题的一些命中,但没有把它们绑在一起。我发现第二个错误消息隐含可能是clang的问题和语句中的逗号数,表明即使额外的逗号是在 NSMutableString 消息内,仍然被 NSLocalizedString 视为一个额外的参数。这是否有意义?

Searching here and elsewhere I haven't found an explanation. I read through a number of hits on each error message and various NSLocalizedString issues, but nothing that tied them together. What I found about the second error message implied maybe an issue with clang and the number of commas in the statement, indicating that even though the extra comma is within the NSMutableString message, it was still being seen as an extra parameter by NSLocalizedString. Does that make any sense?

这个问题不重要,但是语句用于设置导航栏标题的本地化版本,基于从字典,其对于不同的视图而变化。 NSMutableString 部分定义了基于英语标题的本地化注释。

Not important for the question, but the statement is intended to set the localized version of a navigation bar title based on the English version pulled from a dictionary, which varies for different views. The NSMutableString portion defines the comment for the localization based on the English title.

编辑:在解决此问题后接受的答案,下面,我注意到另一个相关的问题。 localString 的声明产生了一个未使用的变量编译器警告,虽然它被清楚地使用。这也是由于它在一个C宏内,为了完整性,我在这里添加一个链接到相关的帖子关于抑制这个警告:

After resolving this issue per the accepted answer, below, I noticed another related issue. The declaration of localString was producing an "Unused variable" compiler warning, though it was clearly being used. This is also due to it being within a C-macro and for completeness, I'm adding a link to a relevant post here about suppressing that warning: How can I get rid of an "unused variable" warning in Xcode

推荐答案

我相信这是一个错误的C宏扩展的结果。事实上,如果你写:

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

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

这将编译。由于某些原因,预处理器不能很好地处理 [] 中的文本(可能是因为它不知道Objective-C调用),并且处理<$ c中的所有元素$ c> [] 作为单独的参数:

this will compile. For some reason, the preprocessor is not handling well the text within [] (possibly because it does not know about Objective-C calls) and treats all the elements within [] as separate arguments:

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

PS:当我写错误的C宏扩展时,我的意思是

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


宏扩展是一个棘手的操作,充斥着令人讨厌的角落情况和情况,它们渲染你认为是一种漂亮的方法来优化预处理器的扩展算法错误相当微妙的方式。

Macro expansion is a tricky operation, fraught with nasty corner cases and situations that render what you thought was a nifty way to optimize the preprocessor's expansion algorithm wrong in quite subtle ways.

这篇关于NSLocalizedString - 使用未声明的标识符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆