字符串资源 Xcode + swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25249555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:22:11  来源:igfitidea点击:

String resources Xcode + swift

iosxcodemacosswift

提问by mr_ivan777

I am new in iOS developing and in Swift language. And I try to make simple iOS application, and I need to have some string resources (for labels and text fields) in app. Of course, I can put this strings to my *.swift files as constants, but I think, it is a bad way. How can I do it? I need something as string resources in Android. I use Xcode 6 beta with swift.

我是 iOS 开发和 Swift 语言的新手。我尝试制作简单的 iOS 应用程序,我需要在应用程序中有一些字符串资源(用于标签和文本字段)。当然,我可以把这个字符串作为常量放到我的 *.swift 文件中,但我认为,这是一种糟糕的方式。我该怎么做?我需要一些东西作为 Android 中的字符串资源。我使用 Xcode 6 beta 快速。

TY for answers ;)

TY的答案;)

采纳答案by bmeulmeester

Your best bet is creating a file called Localizable.strings. In this file you can make localised string variables you can use throughout your application. Define strings like this;

最好的办法是创建一个名为Localizable.strings. 在此文件中,您可以制作可在整个应用程序中使用的本地化字符串变量。像这样定义字符串;

"hello_world" = "Hello World!";

Use them in your Obj/C code like so;

像这样在你的 Obj/C 代码中使用它们;

NSLocalizedString(@"hello_world", nil);

Or in Swift;

或者在 Swift 中;

NSLocalizedString("hello_world", comment: "")

p.s., I haven't tested this. Swift code might be faulty because of the fact I can't test this atm.

ps,这个我没测试过。Swift 代码可能有问题,因为我无法测试这个 atm。