Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You create your parser, but you don't parse. Let's assume you created your parser like so:</p> <pre><code>NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL]; </code></pre> <p>You then need to set the delegate and initiate the parse:</p> <pre><code>parser.delegate = self; [parser parse]; </code></pre> <p>You then have to write your <a href="http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html" rel="nofollow"><code>NSXMLParserDelegate</code></a> methods. (For more information on <code>NSXMLParser</code>, see the <a href="http://developer.apple.com/library/ios/documentation/cocoa/Conceptual/XMLParsing/XMLParsing.html#//apple_ref/doc/uid/10000186i" rel="nofollow">Event-Driven XML Programming Guide</a>.) E.g., if you only need temperature, you could write a <code>didStartElement</code> method:</p> <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"yweather:condition"]) { // do whatever you want with `attributeDict`, perhaps saving it in some class property; I'm just going to log it NSLog(@"current condition = %@", attributeDict); } } </code></pre> <p>Note, the city should be a WOEID. (See the <a href="http://developer.yahoo.com/weather/" rel="nofollow">API description</a>.) So, instead of a location of:</p> <pre><code>NSString *location = @"Palermo"; </code></pre> <p>You should use (for Palermo in Italy):</p> <pre><code>NSString *location = @"719846"; </code></pre> <hr> <p>By the way, I agree with Caleb that you should either do this <code>initWithContentsOfURL</code> in a background queue, or you should otherwise retrieve the <code>NSData</code> using some asynchronous mechanism.</p> <p>For example, you could do:</p> <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString *location = @"719846"; NSString *temperatureUnit = @"c"; NSString *address = @"http://weather.yahooapis.com/forecastrss?w="; NSString *request = [NSString stringWithFormat:@"%@%@&amp;u=%@",address,location, temperatureUnit]; NSURL *URL = [NSURL URLWithString:request]; NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL]; parser.delegate = self; [parser parse]; }); </code></pre> <p>If you do this in the background queue, make sure to dispatch UI updates back to the main queue, e.g.:</p> <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"yweather:condition"]) { // UI updates should be dispatched back to the main queue, e.g.: dispatch_async(dispatch_get_main_queue(), ^{ NSString *temp = attributeDict[@"temp"]; self.tempLabel.text = temp; }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload