tableViewtableView cellForRowAtIndexPathindexPath CellIdentifier ; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; *daysWeather; daysWeather = [self.weather currentCondition]; break; upcomingWeather = [self.weather upcomingWeather]; daysWeather = [upcomingWeather objectAtIndex:indexPath.row]; ; } cell.textLabel.text = [daysWeather weatherDescription]; // maybe some code will be added here later... return cell; }
跟tableView:numberOfRowsInSection: 方法一样,在这里使用了便利的NSDictionary categories来获得数据。当前天的天气是一个字典,而未来几日的天气则存储在一个数组中。
生成并运行工程,然后点击JSON按钮. 这将会动态的获得一个AFJSONOperation对象, 并看到如下画面内容:
JSON 操作成功! 操作Property Lists(plists)
Property lists (或简称为 plists) 是以确定的格式(苹果定义的)构成的XML文件。苹果一般将plists用在用户设置中。看起来如下:
<dict> <key>data</key> <dict> <key>current_condition</key> <array> <dict> <key>cloudcover</key> <string>16</string> <key>humidity</key> <string>59</string>上面的意思是:
现在是时候加载plist版本的天气数据了!找到plistTapped: 方法,并用下面的实现替换:
senderweatherUrl stringWithFormat,BaseURLString]; URLWithString:weatherUrl]; requestWithURL:url]; AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request successrequest, self.weather propertyList; self.title ; [self.tableView reloadData]; } failurerequest, UIAlertView UIAlertView allocmessagestringWithFormat,error] delegate:nil cancelButtonTitleotherButtonTitles; [av show]; }]; [operation start]; }
注意到,上面的代码几乎与JSON版的一致,只不过将操作(operation)的类型从AFJSONOperation 修改为 AFPropertyListOperation. 这非常的整齐:你才程序只需要修改一丁点代码就可以接收JSON或plist格式的数据了!
生成并运行工程,然后点击PLIST按钮。将看到如下内容:
如果你需要重置所有的内容,以重新开始操作,导航栏顶部的Clear按钮可以清除掉title和table view中的数据。 操作XML
AFNetworking处理JSON和plist的解析使用的是类似的方法,并不需要花费太多功夫,而处理XML则要稍微复杂一点。下面,就根据XML咨询构造一个天气字典(NSDictionary)。
iOS提供了一个帮助类:NSXMLParse (如果你想了解更多内容,请看这里的链接:).
还是在文件WTTableViewController.m, 找到 xmlTapped: 方法,并用下面的实现替换:
senderweatherUrl stringWithFormat,BaseURLString]; URLWithString:weatherUrl]; requestWithURL:url]; AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request successrequest, XMLParser.delegate = self; ; [XMLParser parse]; } failurerequest, UIAlertView UIAlertView allocmessagestringWithFormat,error] delegate:nil cancelButtonTitleotherButtonTitles; [av show]; }]; [operation start]; }
到现在为止,这看起来跟之前处理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后面:
parserparser didStartElementelementName namespaceURInamespaceURI qualifiedNameqName attributesattributeDict; parserparser foundCharacters; parserparser didEndElementelementName namespaceURInamespaceURI qualifiedNameqName; parserDidEndDocumentparser;
为了支持资讯的解析,还需要一些属性来存储相关的数据。将下面的代码添加到@implementatio后面:
xmlWeather; strongcurrentDictionary; strongpreviousElementName; elementName; outstring;
接着打开WTTableViewController.m,现在你需要一个一个的实现上面所说的几个delegate方法。将下面这个方法粘贴到实现文件中: