NSString *string = @"Product: Acme Potato Peeler; Cost: 0.9873\nProduct: Chef Pierre Pasta Fork; Cost: 0.75 19\nProduct: ChefPierre Colander; Cost: 1.27 2\n";
NSCharacterSet *semicolonSet;
NSScanner *theScanner;
NSString *PRODUCT = @"Product:";
NSString *COST = @"Cost:";
NSString *productName;
float productCost;
NSInteger productSold;
semicolonSet = [NSCharacterSetcharacterSetWithCharacte rsInString:@";"];
theScanner = [NSScanner scannerWithString:string];
while ([theScanner isAtEnd] == NO)
{
if([theScanner scanString:PRODUCT intoString:NULL]&&
[theScanner
scanUpToCharactersFromSe t:semicolonSet
intoString:&productName]&&
[theScanner
scanString:@";" intoString:NULL]&&
[theScanner
scanString:COST intoString:NULL]&&
[theScanner
scanFloat:&productCost]&&
[theScanner
scanInteger:&productSold])
{
NSLog(@"Sales
of %@: $%1.2f", productName, productCost *productSold);
}
}
NSCharacterSet *semicolonSet;
NSScanner *theScanner;
NSString *PRODUCT = @"Product:";
NSString *COST = @"Cost:";
NSString *productName;
float productCost;
NSInteger productSold;
semicolonSet = [NSCharacterSetcharacterSetWithCharacte
theScanner = [NSScanner scannerWithString:string];
while ([theScanner isAtEnd] == NO)
{
}