问题描述
是否有 NSLocalizedString(...)
的 Swift 等价物?在Objective-C
中,我们通常使用:
Is there an Swift equivalent of NSLocalizedString(...)
?In Objective-C
, we usually use:
NSString *string = NSLocalizedString(@"key", @"comment");
如何在 Swift 中实现同样的效果?我找到了一个函数:
How can I achieve the same in Swift? I found a function:
func NSLocalizedString(
key: String,
tableName: String? = default,
bundle: NSBundle = default,
value: String = default,
#comment: String) -> String
但是,它很长,一点也不方便.
However, it is very long and not convenient at all.
推荐答案
NSLocalizedString
也存在于 Swift 的世界中.
The NSLocalizedString
exists also in the Swift's world.
func NSLocalizedString(
key: String,
tableName: String? = default,
bundle: NSBundle = default,
value: String = default,
#comment: String) -> String
tableName
、bundle
和 value
参数标有 default
关键字,这意味着我们可以在调用函数时省略这些参数.在这种情况下,将使用它们的默认值.
The tableName
, bundle
, and value
parameters are marked with a default
keyword which means we can omit these parameters while calling the function. In this case, their default values will be used.
由此得出结论,方法调用可以简化为:
This leads to a conclusion that the method call can be simplified to:
NSLocalizedString("key", comment: "comment")
Swift 5 - 没有变化,仍然如此.
Swift 5 - no change, still works like that.
这篇关于Swift 中的 NSLocalizedString 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!