Re: why is nsscanner giving me an extra result?
Re: why is nsscanner giving me an extra result?
- Subject: Re: why is nsscanner giving me an extra result?
- From: Ian Spackman <email@hidden>
- Date: Sun, 23 Jan 2005 22:35:02 +0000
Hi Michael,
On 23 Jan 2005, at 19:51, Michael Hanna wrote:
I'm parsing web results with this method, but I find that the
NSScanner is giving me an extra result, that is, in this example
instance, it should find only one set of data rather than two. The
extra item it creates makes the artistName and the songName the same,
which is really odd. It seems like it's staying in the while-loop one
more iteration than I expect it to.....
any ideas what I'm assuming wrongly here?
Michael
- (void)parseMXTabsSongs
{
NSScanner *scanner = [NSScanner localizedScannerWithString:[self
myRawWebResult]];
NSString *scanResult = nil;
NSString *artistStartTag = @"<font size=\"2\" color=\"CCCCCC\">";
NSString *artistEndTag = @"</a>";
NSString *urlStartTag = @"- <a href=\"";
NSString *urlEndTag = @"\"";
NSString *songStartTag = @">";
NSString *songEndTag = @"</font>";
//remove all objects from the songs array
[songs removeAllObjects];
//NSLog(@"[songs removeAllObjects];");
// scan up to the artist's name
while([scanner scanUpToString:artistStartTag intoString:nil])
Perhaps you are assuming that scanUpToString:intoString: returns YES
only if it finds the search string? That's the assumption I made a
couple of days ago, anyway. After reading the docs, cursing and
googling a bit I reread the docs more pedantically. Actually the method
returns YES if the receiver scans _any_ characters. In this case on the
final iteration the scanner (surprisingly? it surprised me) does scan
some characters. It scans all the characters up to the end of its
string trying (if failing) to match artistStartTag. At that point it
stops scanning and returns:
YES_I_HAVE_SCANNED_SOME_STUFF_ALBEIT_QUITE_PROBABLY_NOT_THE_STUFF_YOU_WE
RE_LOOKING_FOR_BUT_I_CANT_BE_SURE_OF_THAT_SO_I_CHORUS_WITH_THE_DOCS_AND_
SAY_YEAH_YEAH_YEAH_SHE_LOVES_YOU.
Try adding
&& ![scanner isAtEnd]
to your loop test.
{
// skip over the artiststarttag
[scanner scanString:artistStartTag intoString:nil];
// found artist name, store it
[scanner scanUpToString:artistEndTag intoString:&scanResult];
//NSLog(@"scanResult %@", scanResult);
if(scanResult != nil)
{
This looks at first sight as if it will catch the not-found case
anyway. But it won't. The final time through the loop the scanner will
have exhausted the string at the loop test. So it will have done
nothing at
[scanner scanUpToString:artistEndTag intoString:&scanResult];
except return NO. That includes having done nothing to scanResult which
will retain its value from the previous iteration. (The test would have
worked, of course, if scanResult had been declared - or set to nil -
within the loop.)
//NSLog(@"if(scanResult)");
TaoSong *newSong = [[TaoSong alloc] init];
NSLog(@"scanResult artist %@", scanResult);
[newSong setArtistName:scanResult];
// scan up to the URL
[scanner scanUpToString:urlStartTag intoString:nil];
[scanner scanString:urlStartTag intoString:nil];
[scanner scanUpToString:urlEndTag intoString:&scanResult];
NSLog(@"scanResult URL %@", scanResult);
[newSong setTabLocation:[NSURL URLWithString:[NSString
stringWithFormat:@"http://mxtabs.net/%@",scanResult]]];
// scan up to the songStartTag
[scanner scanUpToString:songStartTag intoString:nil];
[scanner scanString:songStartTag intoString:nil];
[scanner scanUpToString:songEndTag intoString:&scanResult];
NSLog(@"scanResult song name %@", scanResult);
[newSong setSongName:scanResult];
// add these items to my songs array
[songs addObject:newSong];
[newSong release];
}
}
// off by one, not sure why, so just delete the last object
//if([songs count] > 1)
// [songs removeLastObject];
[self updateWebSearchTable];
NSLog(@"songs, %@", songs);
}
Cheers!
Ian
P.S. The longish constant quoted above is DEFINE'd as YES in
/usr/include/sosumi.h
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden