Let's assume I have a string ("G00 X0.0000 Y0.0000") and I need to to parse its contents. Here is my code:
NSCharacterSet *params = [NSCharacterSet characterSetWithCharactersInString:@"XY"];
//setup the scanner
NSScanner *scanner = [NSScanner scannerWithString:stringToBeScanned];
NSString *scanned = nil;
//scan the string
NSLog(@"%@", stringToBeScanned);
while ([scanner scanUpToCharactersFromSet:params intoString:&scanned]) {
struct keypair code;
code.key = [scanned characterAtIndex:0];
code.value = [[scanned substringFromIndex:1] doubleValue];
NSLog(@"--> %@ [%lu]= (%c, %.4f)", scanned, [scanner scanLocation], code.key, code.value);
}
And the output to NSLog:
G00 X0.0000 Y0.0000
--> G00 [4]= (G, 0.0000)
My characterSet includes both 'X' and 'Y' and I can't figure out why my NSScanner won't scan in the 'X0.0000 ' - it should find that Y and pull in everything from X up to Y according to my understanding.
I can see from the scanLocation that the scanner is stopping at index 4 (correctly), but the loop either doesn't continue or evaluates to false. Shouldn't the scanner keep looping and finding my delimiters (from the characterSet) and grabbing data?