I'm currently making a word game for iOS that, when loading, reads in a text file of around 30000 words and loads them into a prefix tree for quick searching during gameplay. This works well, but the loading and tree construction process adds a noticeable few seconds to the app's startup time. At the moment I'm testing on an iPhone 4, but I imagine it would be a good deal slower on a 3GS earlier models.
Is there a way to create this tree at compile time rather than when the app opens? Or, less ideally, would it开发者_开发问答 be possible to prebake the data with another program and add that file to the project instead of doing it at runtime? How would I go about doing that?
I run into the same issue with a game we developed, and for us, it performed better to use a SQLite DB with the words instead of the in-memory tree. The DB used less space than the plist that represented the tree, it did not require us to preload it in memory, and the performance (when querying for a valid word) was about the same.
pgb's answer is good. If you do not want to use SQLite, you can store your data in a plist and have [NSDictionary dictionaryWithContentsOfFile:] create a tree for you.
If you do choose to have the data compiled into your program, it will have to be built of primitive types, such as numbers and characters. Then, use structures and arrays to define the structure, and use a constant variable to store the data. Here is a simple example which is just an array of character strings:
const char *words[] = {"Word1","Word2","Word3"};
const unsigned numWords = (sizeof(words) / sizeof(char*));
精彩评论