我有一个显示 XML 提要结果的 Collection View 。它工作得很好,但我需要在本地保存 XML 提要的结果(例如,如果没有可用的互联网连接)。
我知道如何在本地保存原始提要,但我认为我真正需要做的是遍历 XML 提要中的项目并将每个项目保存为 NSData 项目。
这是正确的思考方式吗?或者您有什么有用的建议或技巧吗?
- (void)startParsing
{
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/main/rss/hotnews/hotnews.rss#sthash.TyhRD7Zy.dpuf"]];
[xmlparser setDelegate:self];
[xmlparser parse];
if (_marrXMLDataCollection.count != 0) {
// Save XML to NSData items in background?
[self.collectionView reloadData];
}
}
这是一个提要的示例,我也将其作为 localdata.plist
存储在 Documents 目录中:
(
{
link = "\nhttp://www.apple.com/pr/library/2016/04/26Apple-Reports-Second-Quarter-Results.html?sr=hotnews.rss";
pubDate = "\nTue, 26 Apr 2016 14:44:21 PDT";
title = "\nApple Reports Second Quarter Results";
},
{
link = "\nhttp://www.apple.com/final-cut-pro/in-action/trim-editing/?sr=hotnews.rss";
pubDate = "\nWed, 20 Apr 2016 10:05:59 PDT";
title = "\n\nFinal Cut Pro X helps small company delight world\U2019s biggest clients";
},
{
link = "\nhttp://www.apple.com/ipad-pro/?sr=hotnews.rss";
pubDate = "\nMon, 21 Mar 2016 12:00:03 PDT";
title = "\n\nApple Introduces 9.7-inch iPad Pro";
},
{
link = "\nhttp://www.apple.com/iphone-se/?sr=hotnews.rss";
pubDate = "\nMon, 21 Mar 2016 11:58:09 PDT";
title = "\n\nApple Introduces iPhone SE \U2014 the Most Powerful Phone with a 4-inch Display";
},
最佳答案
您可以将 XML 直接保存到一个文件中,可以是二进制数据,也可以是文本。您无需将其解压缩并将其转换为任何内容。稍后您可以加载罚款并重新运行您的解析代码。这是最少的代码选项。它的运行时成本更高,但除非您的 XML 文件相当大,否则您不会注意到差异。
如果你想保存处理过的内容,那么你的 XML 应该变成一个模型对象数组,你可以在这些模型对象上实现 NSCoding
以允许你将它们存档到磁盘。这应该会提高运行时效率,但也需要更多代码。
关于ios - 枚举 XML 提要中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38231144/