【问题标题】:how to create macro as function with swift programming language如何使用快速编程语言将宏创建为函数
【发布时间】:2016-07-26 08:23:07
【问题描述】:

以下语句表示在目标c中定义宏。

据我所知,Swift 不支持宏,所以我必须为此使用函数,所以你能帮我提供以下语句的 swift 代码吗?

#define LOCAL_STRING(KEY, VALUE, ...) [NSString localizedStringWithFormat:NSLocalizedString(@KEY, !@VALUE), ##__VA_ARGS__]

【问题讨论】:

标签: ios objective-c iphone xcode swift


【解决方案1】:

尝试使用字符串扩展(I found here 的答案):

extension String {
      func localizedStringWithVariables(value: String, vars: CVarArgType...) -> String {
        return String(format: NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: value, comment: ""), arguments: vars)
      }
}

那么你可以这样做:

"KeyNameHere".localizedStringWithVariables("some default value", vars: [])

附言如果键/值中没有格式参数,则此 vars 示例中的空数组应该没问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2011-02-23
    • 2010-11-27
    • 2011-04-20
    相关资源
    最近更新 更多