Data format for content heavy iPhone app - Plist or XML?
I'm building an iPhone app that is essentially a book, it will be bundle开发者_C百科d with a lot of text-heavy content.
I considered bundling the data as XML and load it when the application starts but the XML would contain a lot of nested structures and be a bit of a pain to parse.
Would it be better to use a plist? I'm concerned about memory usage and plists are loaded entirely into memory - can they be parsed in chunks? Is there a maximum size to a plist and how efficient are they?
I'm not sure how big the bundled content is going to be yet but I should imagine it could be anywhere from 500k to 4MB.
Property Lists are the native serialization format for anything derived from NSObject. There are some issues with mutable-state preservation, but overall plist is the format preferred by Apple.There are parsing methods available with NSData that abstract away the details of the mark-up. As for XML, you're burdened with the task of writing your own parser.
You can refer to this for further details.
It should be noted that a plist file itself is strict XML; in choosing plist, on the server-side you should be able to parse the plist XML and treat every two nodes as key-value pairs.
In my experience, plist is far easier for you to maintain and better in the long run. I have a similar situation and wrote an app that generates the data file my app reads using all of the same APIs with no extra setup. Its hitting the ground running (as long as you're familiar with Cocoa.)
Its even human readable with the property List Editor app included in the iPhone SDK. Although I don't recommend it for very large data structures by hand, that's why I mentioned how I built another app for that — the code generating the data and using it almost has a 1:1 comparison, they're that similar. The plist editor does come in very handy to tweak an item or edit small-medium data and, once again, it hides the underlying XML.
If human-readability is a design goal, you should consider JSON. It's not the right answer for every application, but it bears considering. The open-source json-framework (here: http://code.google.com/p/json-framework/) is GREAT, and provides very convenient methods for encoding and decoding JSON strings to objects, as a category on NSString. So you can say:
NSString *jsonString = ...// however you're loading the goods
NSDictionary *myData = [jsonString JSONValue];
...and boom, you're working with native objects. For my money, that's even easier than reading a plist.
Why not ship the content as HTML? (Maybe split into chapters or some such)
That way you could immediately display it through a WebView without any parsing or reformatting.
精彩评论