• 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
RE: NSScanner know-how
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: NSScanner know-how


  • Subject: RE: NSScanner know-how
  • From: "Jonathan E. Jackel" <email@hidden>
  • Date: Fri, 25 Jul 2003 18:26:39 -0400

You're getting there, but you still don't really understand how a scanner
works. The first rule is, scanners are incredibly stupid! You have to do
all the thinking.

In your original code:

> NSString* id_origin = @"#id:";

and you have modified the rest to read:


> while ([scanner isAtEnd] == NO)
> {
> okay = [scanner scanString:id_origin intoString:NULL];
> NSLog(@"Scan location:%d", [scanner scanLocation]);
> okay = [scanner scanUpToCharactersFromSet:semicolonSet
> intoString:&id_sql];
> NSLog(@"id = %@", id_sql);
> okay = [scanner scanString:@";" intoString:NULL];
> }

and your data looks like this:

> #id:15;
> #customer:F.lli Remari;
> #phone:123456789;
> #cell:13456;
> #fax:987987;
> #id:16;
> #customer:Zanovello Nicola;
> #phone:456789;
> #cell:;
> #fax:12356;


The first time through the loop, the scanner looks for "#id:" at the current
scan location, which is the beginning of your string. The scanner finds it,
scans it, and advances the scan location to the character after the colon.
Then you tell the scanner to scan up to the next semicolon and put
everything between the colon and the semicolon in id_sql. Then you scan
past the semicolon. Now your scanner is at the character right after "15;"
and your loop starts over.

The scanner looks for "#id:" at the current scan location, which is now the
end of the first line. The scanner doesn't find it (you could test this by
NSLogging the value of okay) and the scan location stays the same. Then you
tell the scanner to scan up to the next semicolon and put everything between
the scan location and the semicolon in id_sql. That's how you get "
#customer:F.lli Remari" in your log. Then you scan past the semicolon.
etc., etc.,

What you want to do is more like this:

BOOL okay = YES;
NSString *colon = @":", *semi = @";", *id_sql;
NSScanner *scanner = [NSScanner scannerWithString:yourString];
while ([scanner isAtEnd] == NO && okay == YES)
{
okay = [scanner scanUpToString:colon intoString:NULL];
okay = [scanner scanString:colon intoString:NULL];
okay = [scanner scanUpToString:semi intoString:&id_sql];
NSLog(@"id = %@", id_sql);
okay = [scanner scanString:semi intoString:NULL];
}

Jonathan
_______________________________________________
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.

References: 
 >Re: NSScanner know-how (From: Sanri Parov <email@hidden>)

  • Prev by Date: less font families
  • Next by Date: NSWindow comes up as a tiny little window
  • Previous by thread: Re: NSScanner know-how
  • Next by thread: RE: NSScanner know-how
  • Index(es):
    • Date
    • Thread