• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSScanner baffling behaviour (scanString:intoString: doesn't work!)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSScanner baffling behaviour (scanString:intoString: doesn't work!)


  • Subject: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
  • From: Greg Hurrell <email@hidden>
  • Date: Tue, 8 Apr 2003 19:21:14 +0930

Can anyone tell me why the following method isn't working?

It's supposed to scan a string for material between "startTag" and "endTag". For example, calling:

[[self class] _scanString:@"<p>This is some <b>HTML</b> code!</p>" startTag:@"<b>" endTag:@"</b>"]

Is supposed to return the first instance of text between "<b>" and "</b>" (ie. "HTML").

// scan a string (source) for material between startTag and endTag
+ (NSString *)_scanString:(NSString *)source
startTag:(NSString *)startTag
endTag:(NSString *)endTag
{
NSScanner *scanner = [NSScanner scannerWithString:source];
NSString *result = nil;

// first try for startTag
if (![scanner scanString:startTag intoString:nil])
// startTag not found!
return nil;

// then end tag
if ([scanner scanUpToString:endTag intoString:&result])
// known bug: if endTag doesn't appear in source string, will return entire rest of string!
return result;
else
return nil;
}

What actually happens when I run this code? It returns nil because startTag is never found.

Apple's NSScanner docs say (of scanString:intoString:), "Invoke this method with nil as stringValue to simply scan past a given string."

Sounds simple, but it doesn't work. It appears to never find the startTag, even when I know it's in there. After the first attempt at scanning for the startTag the scanLocation is still at 0.

Other things I've tried in my efforts to scan pass "startTag":

while ([!scanner isAtEnd])
{
if ([scanner scanString:startTag intoString:nil])
{
NSLog(@"OMG! I found the start tag"); // this never happens
break;
}
else
{
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}

Things I've noticed: if I repeatedly invoke scanString:intoString: the scanLocation never moves, and so I wind up in an infinite loop; that's why I added the manual increment of the scanLocation, but according to the docs (as I interpret them) this definitely should not be necessary! It doesn't matter anyway because this alternative method never finds the startTag either.

Thanks for any light you can shed on this...

Cheers
Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
      • From: Bill Cheeseman <email@hidden>
    • Re: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
      • From: Greg Titus <email@hidden>
    • Re: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
      • From: j o a r <email@hidden>
  • Prev by Date: NSImageView Problem
  • Next by Date: Re: How-To: private framework
  • Previous by thread: Re: NSImageView Problem
  • Next by thread: Re: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
  • Index(es):
    • Date
    • Thread