Reputation: 1797
I am making an Iphone drinking card game app. All the card mean something different and i want the user to be able to press an info button and then show a new screen with information about the current card. How can i make a document to load text from instead of using a bunch og long strings?
Thanks
Upvotes: 0
Views: 268
Reputation: 18253
In my other answer, I suggest the use of a dictionary if your texts are mostly to be considered as data. However, if your strings are UI elements (alert texts, window titles, etc.) you might want to look into strings
files and NSBundle
's support for them.
Strings files are ideally suited for localization, the format is explained here.
To read them into you app, use something like this:
NSString *text1 = NSLocalizedStringFromTable(@"TEXT1", @"myStringsFile", @"Comment");
If you call your file Localizable.strings
, you can even use a simpler form:
NSString *str1 = NSLocalizedString(@"String1", @"Comment on String1");
A useful discussion here - a bit old, but still useful.
Upvotes: 0
Reputation: 18253
You could look into plist files - they can be loaded quite easily into the various collection objects and edited with the plist editor in Xcode.
For instance, if you organize your data as a dictionary, the convenience constructor
+ (id)dictionaryWithContentsOfURL:(NSURL *)aURL
from NSDictionary would provide you with as many easily accessible strings as you need.
This method is useful if you consider your strings primarily data as opposed to UI elements.
Update:
As @Alex Nichol suggested, here is how you can do it in practice:
To create a plist file:
en.lproj
, to aid in localizationuser1
) and a value (for instance "Joe")To read the contents:
NSURL *plistURL = [[NSBundle mainBundle] URLForResource:@"Property List" withExtension:@"plist"];
NSLog(@"URL: %@", plistURL);
NSDictionary *strings = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSString *user1 = [strings objectForKey:@"user1"];
NSLog(@"User 1: %@", user1);
Upvotes: 3
Reputation: 47759
A plist, a JSON string, and an SQLite database walked into a bar ...
Oops!! I mean those are the three most obvious alternatives. The JSON string is probably the easiest to create and "transport", though it's most practical to load the entire thing into an NSDictionary and/or NSArray, vs read from the file as each string is accessed.
The SQLite DB is the most general, and most speed/storage efficient for a very large number (thousands) of strings, but it takes some effort to set it up.
Upvotes: 0