Let’s say I’m using NSXMLParser to load a level (stored as an XML document, obviously) into my iPhone game. NSXMLParser works by assigning a delegate and sending it messages at key moments. How do I ensure that my entire level is loaded before doing anything else? I know I can make my main class the delegate and implement parserDidEndDocument, but this feels very hacky. My main class shouldn’t have to know anything about the way the parsing is done! On the other hand, if I make a separate class/delegate for parsing my level, my main class has no way of knowing when the parsing is finished, unless it queries the parsing class continuously or the parsing class sends it a message. Either way, the main class would be tied to the implementation of the parsing class.
Can I hide all this event-driven business from the main class and simply make the parser return the level object when it’s done? (i.e., newLevel = [[GameLevel alloc] initFromXML:xmlfile], which would in turn use NSXMLParser to load the level and then somehow return when finished.) At the moment, I’m using an external DOM parser, but I’m curious how this would be done with NSXMLParser.
Sorry if this is a stupid question — I’m a bit new to this!
EDIT: According to the NSXMLParser class reference, the parse method, which “starts the event-driven parsing operation”, returns YES if successful and NO if there’s an error. Does this mean that it returns only after the parsing has finished? This would solve my problem, as I had assumed that parsing continued even after the parse method returned.
NSXMLParser blocks until the parsing is done. You don’t have to do anything because the level will be loaded after the parse method returns.