我对编码很新,我正在制作RSS Feed类型的应用,并想知道是否有搜索关键字的方式,例如" facebook"或"新闻"然后只处理包含关键字的项目到我的UITableView。
以下是我到目前为止从代码中检索描述的代码。
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
link = [NSMutableString stringWithString:
[link stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
pubDate = [NSMutableString stringWithString:[pubDate stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
[item setObject:author forKey:@"author"];
[item setObject:link forKey:@"link"];
[item setObject:description forKey:@"description"];
[item setObject:pubDate forKey:@"pubDate"];
item[@"pubDateObj"] = [self dateFromXml:item[@"pubDate"]];
NSComparator comparator = ^(NSDictionary *a, NSDictionary *b) {
return [b[@"pubDateObj"] compare:a[@"pubDateObj"]];
};
NSUInteger index = [feeds indexOfObject:item
inSortedRange:NSMakeRange(0, [feeds count])
options:NSBinarySearchingInsertionIndex
usingComparator:comparator];
[feeds insertObject:[item copy] atIndex:index];
}
}
答案 0 :(得分:0)
当然,替换:
[feeds insertObject:[item copy] atIndex:index];
使用:
NSString *strToEvaluate = item[@"description"];
NSString *word = @"facebook";
NSString *word2 = @"news";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", word];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", word2];
if([predicate evaluateWithObject:strToEvaluate] || [predicate2 evaluateWithObject:strToEvaluate]) {
[feeds insertObject:[item copy] atIndex:index];
}
或者您也可以使用regularExpression
。