Re: NSScanner Sanity Check
Re: NSScanner Sanity Check
- Subject: Re: NSScanner Sanity Check
- From: mmalc crawford <email@hidden>
- Date: Tue, 8 May 2007 23:35:10 -0700
On May 8, 2007, at 10:47 AM, James Hillhouse wrote:
// meanRadius
[scanner scanUpToCharactersFromSet:[NSCharacterSet
alphanumericCharacterSet]
intoString:nil];
[scanner scanDouble:&mR];
stringOfElements = [[NSNumber numberWithDouble:mR]
stringValue];
It's not clear why you're going to all this work to skip whitespace.
The documentation (<http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/Scanners.html
>) states "Scanners skip whitespace and newline characters by
default"...
(See also in the Example, "Note that because a scanner skips
whitespace and newlines by default, the loop does no special
processing for them.")
Consider this slightly amended version of the example:
Suppose you have a string containing lines such as:
Product: Acme Potato Peeler; Cost: 0.98 93
Product: Chef Pierre Pasta Fork; Cost: 0.75 24
Product: Chef Pierre Colander; Cost: 1.27 17
NSString *string = @"Product: Acme Potato Peeler; Cost: 0.98
93\nProduct: Chef Pierre Pasta Fork; Cost: 0.75 24\nProduct: Chef
Pierre Colander; Cost: 1.27 17";
NSCharacterSet *semicolonSet;
NSScanner *theScanner;
NSString *PRODUCT = @"Product:";
NSString *COST = @"Cost:";
NSString *productName;
float productCost;
int productNumber;
semicolonSet = [NSCharacterSet characterSetWithCharactersInString:@";"];
theScanner = [NSScanner scannerWithString:string];
while ([theScanner isAtEnd] == NO) {
if ([theScanner scanString:PRODUCT intoString:NULL] &&
[theScanner scanUpToCharactersFromSet:semicolonSet
intoString:&productName] &&
[theScanner scanString:@";" intoString:NULL] &&
[theScanner scanString:COST intoString:NULL] &&
[theScanner scanFloat:&productCost] &&
[theScanner scanInt:&productNumber]
)
{
/* Do something with productName and productCost. */
NSLog(@"%d at %1.2f = %1.2f", productNumber, productCost,
productNumber * productCost);
}
else
{
NSLog(@"Something broke!");
}
}
Given data of the form:
1685 Toro 54200 1.36715236 0.435902434 9.3806221 127.0577982
274.3439612 211.3461583 0.7712 1.96 1.60 14.23
you can scan the whole file as follows.
mmalc
NSString *sourcePath = /* whatever */ ;
NSError *error;
NSString *source = [NSString stringWithContentsOfFile:sourcePath
encoding:NSUTF8StringEncoding error:&error];
if (source == nil)
{
// log error
}
NSScanner *theScanner = [NSScanner scannerWithString:source];
int bodyNumber;
NSString *bodyName;
int someInt;
float semiMajorAxis;
float eccentricity;
float inclination;
float argumentOfPerihelion;
float longitudeAscendingNode;
float meanAnomoly;
float perihelion;
float aphelion;
float oldCrossSection;
float absoluteMagnitude;
NSCharacterSet *letterCharacterSet = [NSCharacterSet
letterCharacterSet];
while ([theScanner isAtEnd] == NO) {
if ([theScanner scanInt:&bodyNumber] &&
[theScanner scanCharactersFromSet:letterCharacterSet
intoString:&bodyName] &&
[theScanner scanInt:&someInt] &&
[theScanner scanFloat:&semiMajorAxis] &&
[theScanner scanFloat:&eccentricity] &&
[theScanner scanFloat:&inclination] &&
[theScanner scanFloat:&argumentOfPerihelion] &&
[theScanner scanFloat:&longitudeAscendingNode] &&
[theScanner scanFloat:&meanAnomoly] &&
[theScanner scanFloat:&perihelion] &&
[theScanner scanFloat:&aphelion] &&
[theScanner scanFloat:&oldCrossSection] &&
[theScanner scanFloat:&absoluteMagnitude]
)
{
NSLog(@"%d %@: %d, %1.2f, %1.2f, %1.2f, %1.2f, %1.2f, %1.2f, %1.2f,
%1.2f, %1.2f, %1.2f",
bodyNumber, bodyName, someInt, semiMajorAxis, eccentricity,
inclination, argumentOfPerihelion, longitudeAscendingNode,
meanAnomoly, perihelion, aphelion, oldCrossSection, absoluteMagnitude);
}
else
{
NSLog(@"Something broke!");
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden