01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Important protocols<br />

161<br />

Listing 7.13<br />

NSXMLParser methods<br />

- (void)parser:(NSXMLParser *)parser<br />

didEndElement:(NSString *)elementName<br />

namespaceURI:(NSString *)namespaceURI<br />

qualifiedName:(NSString *)qName<br />

{<br />

NSLog(@"Found an element named: %@ with a value of: %@",<br />

elementName, element);<br />

}<br />

This method is called when the parser sees anything between an element’s beginning<br />

and end. You use this entry point as a way to collect all the characters that are between<br />

an XML element beginning and ending: you call the appendString method on the<br />

NSMutableString (listing 7.14). By doing this every time the parser:foundCharacters:<br />

method is called, by the time the parser:didEndElement method is called, the<br />

NSMutableString will be complete. In this method, you first make sure you’ve initialized<br />

your NSMutableString element, and then you append the string you’ve provided,<br />

shown in the next listing.<br />

Listing 7.14<br />

NSXMLParser methods<br />

- (void)parser:(NSXMLParser *)parser<br />

foundCharacters:(NSString *)string<br />

{<br />

if (element == nil)<br />

element = [[NSMutableString alloc] init];<br />

[element appendString:string];<br />

}<br />

Now all that’s left to do is create an instance of your parser and watch it go. Go to<br />

Parser_ProjectAppDelegate.m and add the code shown in the following listing into<br />

the already existing method.<br />

Listing 7.15<br />

Initializing the Parser<br />

- (BOOL)application:(UIApplication *)application<br />

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions<br />

{<br />

self.window.rootViewController = self.viewController;<br />

[self.window makeKeyAndVisible];<br />

}<br />

Parser *parser = [[Parser alloc] init];<br />

return YES;<br />

If you run the application and bring up the terminal window (Shift-Command-R), the<br />

output shown in the following listing should be generated.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!