操作Property Lists(plists)
Property lists (或简称为 plists) 是以确定的格式(苹果定义的)构成的XML文件。苹果一般将plists用在用户设置中。看起来如下:
data current_condition cloudcover 16 humidity 59
上面的意思是:
- 一个字典中有一个名为“data”的key,这个key对应着另外一个字典。
- 这个字典有一个名为 “current_condition” 的key,这个key对应着一个array.
- 这个数组包含着一个字典,字典中有多个key和values。比如cloudcover=16和humidity=59.
现在是时候加载plist版本的天气数据了!找到plistTapped:
-(IBAction)plistTapped:(id)sender{ NSString *weatherUrl = [NSString stringWithFormat:@"%@weather.php?format=plist",BaseURLString]; NSURL *url = [NSURL URLWithString:weatherUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; |
注意到,上面的代码几乎与JSON版的一致,只不过将操作(operation)的类型从AFJSONOperation 修改为 AFPropertyListOperation. 这非常的整齐:你才程序只需要修改一丁点代码就可以接收JSON或plist格式的数据了!
生成并运行工程,然后点击PLIST按钮。将看到如下内容:
操作XML
AFNetworking处理JSON和plist的解析使用的是类似的方法,并不需要花费太多功夫,而处理XML则要稍微复杂一点。下面,就根据XML咨询构造一个天气字典(NSDictionary)。
iOS提供了一个帮助类:NSXMLParse (如果你想了解更多内容,请看这里的链接:SAX parser).
还是在文件WTTableViewController.m, 找到
- (IBAction)xmlTapped:(id)sender{ NSString *weatherUrl = [NSString stringWithFormat:@"%@weather.php?format=xml",BaseURLString]; NSURL *url = [NSURL URLWithString:weatherUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; |
到现在为止,这看起来跟之前处理JSON和plist很类似。最大的改动就是在成功块(success block)中, 在这里不会传递给你一个预处理好的NSDictionary对象. 而是AFXMLRequestOperation实例化的NSXMLParse对象,这个对象将用来处理繁重的XML解析任务。
NSXMLParse对象有一组delegate方法是你需要实现的 — 用来获得XML数据。注意,在上面的代码中我将XMLParser的delegate设置为self, 因此WTTableViewController将用来处理XML的解析任务。
首先,更新一下WTTableViewController.h
@interface WTTableViewController : UITableViewController<NSXMLParserDelegate> |
上面代码的意思是这个类将实现(遵循)NSXMLParserDelegate协议. 下一步将下面的delegate方法声明添加到@implementation后面:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict; - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string; - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName; -(void) parserDidEndDocument:(NSXMLParser *)parser; |
为了支持资讯的解析,还需要一些属性来存储相关的数据。将下面的代码添加到@implementatio后面:
@property(strong) NSMutableDictionary *xmlWeather; //package containing the complete response @property(strong) NSMutableDictionary *currentDictionary; //current section being parsed @property(strong) NSString *previousElementName; @property(strong) NSString *elementName; @property(strong) NSMutableString *outstring; |
接着打开WTTableViewController.m,现在你需要一个一个的实现上面所说的几个delegate方法。将下面这个方法粘贴到实现文件中:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { self.previousElementName = self.elementName; |
当NSXMLParser发现了新的元素开始标签时,会调用上面这个方法。在这个方法中,在构造一个新字典用来存储赋值给currentDictionary属性之前,首先保存住上一个元素名称。还要将outstring重置一下,这个字符串用来构造XML标签中的数据。
然后将下面这个方法粘贴到上一个方法的后面:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if (!self.elementName){ return; } |
如名字一样,当NSXMLParser在一个XML标签中发现了字符数据,会调用这个方法。该方法将字符数据追加到outstring属性中,当XML标签结束的时候,这个outstring会被处理。
继续,将下面这个方法粘贴到上一个方法的后面:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { |
当检测到元素的结束标签时,会调用上面这个方法。在这个方法中,会查找一些标签:
- urrent_condition
元素表示获得了一个今天的天气。会把今天的天气直接添加到xmlWeather字典中。 - weather
元素表示获得了随后一天的天气。今天的天气只有一个,而后续的天气有多个,所以在此,将后续天气添加到一个数组中。 - value
标签出现在别的标签中,所以这里可以忽略掉这个标签。 - weatherDesc
和 weatherIconUrl 元素的值在存储之前,需要需要被放入一个数组中 — 这里的结构是为了与JSON和plist版本天气咨询格式相匹配。 - 所有其它元素都是按照原样(as-is)进行存储的。
下面是最后一个delegate方法!将下面这个方法粘贴到上一个方法的后面:
-(void) parserDidEndDocument:(NSXMLParser *)parser { self.weather = [NSDictionary dictionaryWithObject:self.xmlWeather forKey:@"data"]; self.title = @"XML Retrieved"; [self.tableView reloadData]; } |
当NSXMLParser解析到document的尾部时,会调用这个方法。在此,xmlWeather字典已经构造完毕,table view可以重新加载了。
在上面代码中将xmlWeather添加到一个字典中,看起来是冗余的, 不过这样可以确保与JSON和plist版本的格式完全匹配。这样所有的3种数据格式(JSON, plist和XML)都能够用相同的代码来显示!
-(IBAction)xmlTapped:(id)sender{ ... success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) { // the line below used to be commented out self.xmlWeather = [NSMutableDictionary dictionary]; XMLParser.delegate = self; ... } |
生成和运行工程,然后点击XML按钮,将看到如下内容:
一个小的天气程序
嗯, 上面的这个程序看起来体验不太友好,有点像整周都是阴雨天。如何让table view中的天气信息体验更好点呢?
再仔细看看之前的JSON格式数据:JSON format from before,你会看到每个天气项里面都有一个图片URLs。
AFNetworking给UIImageView添加了一个category,让图片能够异步加载,也就是说当图片在后台下载的时候,程序的UI界面仍然能够响应。为了使用这个功能,首先需要将这个category import到WTTableViewController.m文件的顶部:
#import "UIImageView+AFNetworking.h"
找到tableView:cellForRowAtIndexPath:方法,并将下面的代码粘贴到最后的return cell; 代码上上面(这里应该有一个注释标记)
__weak UITableViewCell *weakCell = cell; |
首先创建一个弱引用(weak)的cell,这样就可以在block中使用这个cell。如果你直接访问cell变量,Xcode会提示一个关于retain循环和内存泄露的警告。
UIImageView+AFNetworking category定义了一个setImageWithURLRequest…
当cell首次被创建的时候,cell中的UIImageView将显示一个占位符图片,直到真正的图片被下载完成。在这里你需要确保占位符的图片与实际图片尺寸大小相同。
如果尺寸不相同的话,你可以在success block中调用cell的setNeedsLayout方法. 上面代码中对两行代码进行了注释,这是因为这里的占位符图片尺寸正好合适,留着注释,可能在别的程序中需要用到。
现在生成并运行工程,然后点击之前添加的3个操作中的任意一个,将看到如下内容: