I'm fairly new to coding and I'm making an RSS feed type app and was wondering if there's a way to search the description for keywords such as "facebook" or "news" and then only process the items which include the keywords to my UITableView.

Here is the code I have so far which retrieves the description from the feed.

- (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];

    }
}
有帮助吗?

解决方案

Sure, replace:

[feeds insertObject:[item copy] atIndex:index];

with:

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];
}

Or you can also use regularExpression.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top