字符串资源Xcode + swift

mr_*_*777 17 macos xcode ios swift

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

TY答案;)

bme*_*ter 17

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

"hello_world" = "Hello World!";
Run Code Online (Sandbox Code Playgroud)

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

NSLocalizedString(@"hello_world", nil);
Run Code Online (Sandbox Code Playgroud)

或者在斯威夫特;

NSLocalizedString("hello_world", comment: "")
Run Code Online (Sandbox Code Playgroud)

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

  • 有关Swift翻译,请参阅(例如)http://stackoverflow.com/questions/25081757/whats-nslocalizedstring-equivalent-in-swift. (2认同)