Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: strings.xml equivalent in iOS app dev?

Tags:

ios

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

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

and accessing it in the java file with

getString(R.string.hello_world);

How can I do something like this with iOS?

like image 615
Kartik_Koro Avatar asked Jun 16 '14 07:06

Kartik_Koro


1 Answers

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):

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.

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!

like image 57
trojanfoe Avatar answered Oct 13 '22 11:10

trojanfoe