简体   繁体   English

iOS:在iOS应用程序开发中的strings.xml等效?

[英]iOS: strings.xml equivalent in iOS app dev?

In the app I'm making I have a lot of huge strings. 在我正在制作的应用程序中,我有很多大字符串。 I don't want to hardcode these into my code because it makes the code unbearably messy. 我不想将这些代码硬编码到我的代码中,因为它会让代码难以忍受。 When I made a similar android app, it was a simple matter of declaring the string in strings.xml as 当我制作类似的Android应用程序时,将strings.xml中的字符串声明为一个简单的问题

<string name="hello_world">Hello World!!</string>

and accessing it in the java file with 并使用。在java文件中访问它

getString(R.string.hello_world);

How can I do something like this with iOS? 我怎么能用iOS做这样的事情?

You could put them into a .plist file and load them using the following code (which assumes a file called strings.plist which has been copied into the app bundle): 您可以将它们放入.plist文件并使用以下代码加载它们(假设一个名为strings.plist的文件已复制到应用程序包中):

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"strings" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSString *string1 = [dict objectForKey:@"string1"];

However if you want to internationalize your app, then using Apple's Internationalization and Localization technology might be something to look at instead. 但是,如果您想要使您的应用程序国际化,那么可能需要使用Apple的国际化和本地化技术。

EDIT : you'll want to load that file/dictionary only once and keep it around the entire app lifetime; 编辑 :您只想加载一次该文件/字典,并将其保存在整个应用程序生命周期内; don't use the above code for every string access! 不要为每个字符串访问使用上面的代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM