Graphics Programs Reference
In-Depth Information
NSRegularExpression
Currently, the table of
RSSItem
s shows each item's title, and this title is an
NSString
that consists of the name of the subforum, the title of the post, and the name of the author:
General Book Discussions :: BNR Books :: Reply by Everyone
It would be easier to browse posts if the table showed just the titles of the posts. To accom-
plish this, we need to parse the full item title, grab the string between the set of double
colons, and set the
title
of each
RSSItem
to be just this string.
In
RSSChannel.h
, declare a new method that will eventually do these things.
@interface RSSChannel : NSObject <NSXMLParserDelegate>
{
NSMutableString *currentString;
}
@property (nonatomic, weak) id parentParserDelegate;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *infoString;
@property (nonatomic, readonly, strong) NSMutableArray *items;
- (void)trimItemTitles;
@end
In
RSSChannel.m
, send this message in
pars-
er:didEndElement:namespaceURI:qualifiedName:
when the channel gives
up control of the parser.
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
currentString = nil;
if ([elementName isEqual:@"channel"])
{
[parser setDelegate:parentParserDelegate];
[self trimItemTitles];
}
}
We could loop through all item titles and write code that parses each string so that only the
title of the post remains, but we can achieve the same result using a regular expression.
First, let's take a closer look at regular expressions and how they work in iOS.