我通常通过解析XML解析博客应用程序,然后排序以获取pubDate并首先显示最新的。为此,我在解析完成时使用它:
NSMutableArray *entries = [NSMutableArray array];
[self parseFeed:doc.rootElement entries:entries];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
for (RSSEntryDirectory *entry in entries) {
int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
RSSEntryDirectory *entry1 = (RSSEntryDirectory *) a;
RSSEntryDirectory *entry2 = (RSSEntryDirectory *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
[_allEntries insertObject:entry atIndex:insertIdx];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
}
}];
我想做的只是从title标签中取出文本,并按字母顺序添加到NSMutableArray。
答案 0 :(得分:1)
[NSArray indexForInsertingObject:sortedUsingBlock:]
是一种未记录的方法,我不建议使用它。
以下是如何做你想做的事:
_allEntries
NSArray *sortedEntries = [_allEntries sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
RSSEntryDirectory *entry1 = (RSSEntryDirectory *) a;
RSSEntryDirectory *entry2 = (RSSEntryDirectory *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
sortedEntries