【问题标题】:Macro for NSLocalizedStringNSLocalizedString 的宏
【发布时间】:2011-12-14 21:06:28
【问题描述】:

我对 NSLocalizedString 的所有调用都将 nil 作为第二个参数,因为我不使用任何 cmets。但我讨厌重复自己。现在我问自己是否可以定义一个像 LSSTRING(str) 这样调用 NSLocalizedString(str, nil) 的宏,如果可以,如何?

【问题讨论】:

    标签: objective-c macros nslocalizedstring


    【解决方案1】:

    你可以。将以下宏添加到您的预编译头文件中。即,在 {ProjectName}-Prefix.pch 文件中

    #define LSSTRING(str) NSLocalizedString(str, nil)
    

    【讨论】:

    • 这种方法的一个问题是 genstrings 期望为评论找到一个字符串,并且会给出如下错误:Bad entry in file ... (line = #): Argument is not a literal string.
    • 在 Xcode 中,只需像这样进行查找/替换: Find: LSSTRING((@\"\w+\")) Replace: NSLocalizedString(\1, \1)
    【解决方案2】:

    我正在使用

    #define _(format, ...) [[NSBundle mainBundle] localizedStringForKey: [NSString stringWithFormat:@"%@", [NSString stringWithFormat:format, ##__VA_ARGS__]] value:@"" table:nil]
    

    【讨论】:

    • 为什么是第一个stringWithFormat:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多