• 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 Sanity Check
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >NSScanner Sanity Check (From: James Hillhouse <email@hidden>)

  • Prev by Date: Re: DO message received on wrong thread
  • Next by Date: Re: KVO using threads
  • Previous by thread: Re: NSScanner Sanity Check
  • Next by thread: How to implement a "Picture Frame" view
  • Index(es):
    • Date
    • Thread