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

NSScanner Sanity Check


  • Subject: NSScanner Sanity Check
  • From: James Hillhouse <email@hidden>
  • Date: Tue, 8 May 2007 12:47:38 -0500

Hey folks! Hope all are doing well.

I was working today on reading keplerian element data files (text, maybe utf-8) using NSString and NSScanner. Here's what I came up with. To see a more readable version, you can go to my posting at http://cocoacoder.org/cocoacoderblog/?p=20#more-20

Here's the problem. I reading in a potentially large data file of keplerian elements, which define an orbit, and I was trying to read from the file the following:

Planet Name
Mean Radius
GravitationalParameter
Eccentricity

This is only a small bit of the info generally included in a keplerian data file. But I figured that if I could read the above info, more info was icing on the cake. I'll go ahead and answer the question of why didn't I use:

[scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
intoString:&stringOfElements];


My main reason was that it didn't work.

What you see below does, actually very nicely.

If what's I've done is not elegant or optimal, could someone chime in and tell me where. This is a hack, the best I could come up with in an afternoon, for an issue that is, like most things in SciTech, very poorly documented by Apple or the Apple developer support sites.

Anyway, enough of that, here's what I came up with:


/* Small sample of Keplerian data containing

Body    Mean Radius mu          eccentricity
============================================
Moon    1738.0      4902.799    0.05490
Earth   6378.1363   398600.4    0.016708617

that is included in a file PlanetaryData.txt in my bundle :-)
*/

- (void)readDataFileAndStore
{

    NSString *bodyName;
    NSString *meanRadius;
    NSString *gravParameter;
    NSString *eccentricity;


double mR; double gP; double ecc;

    NSString *stringOfElements = nil;
    NSString *tmp;

NSString *dataFilePath;
dataFilePath = [[NSBundle mainBundle] pathForResource:@"PlanetaryData"
ofType:@"txt"];


    // This seperates out the lines of data
    NSArray *lines = [[NSString stringWithContentsOfFile:dataFilePath]
                             componentsSeparatedByString:@"\n"];
    NSEnumerator *nse = [lines objectEnumerator];

    NSMutableArray *values = [NSMutableArray new];

    while(tmp = [nse nextObject])
    {
        NSScanner *scanner = [NSScanner scannerWithString:tmp];

// bodyName
[scanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet]
intoString:nil];
[scanner scanCharactersFromSet:[NSCharacterSet alphanumericCharacterSet]
intoString:&stringOfElements];
[values addObject:stringOfElements];


// meanRadius
[scanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet]
intoString:nil];
[scanner scanDouble:&mR];
stringOfElements = [[NSNumber numberWithDouble:mR] stringValue];
[values addObject:stringOfElements];


// gravParameter
[scanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet]
intoString:nil];
[scanner scanDouble:&gP];
stringOfElements = [[NSNumber numberWithDouble:gP] stringValue];
[values addObject:stringOfElements];


// eccentricity
[scanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet]
intoString:nil];
[scanner scanDouble:&ecc];
stringOfElements = [[NSNumber numberWithDouble:ecc] stringValue];
[values addObject:stringOfElements];
}


    bodyName        = [[values objectAtIndex:0] retain];
    meanRadius      = [[values objectAtIndex:1] retain];
    gravParameter   = [[values objectAtIndex:2] retain];
    eccentricity    = [[values objectAtIndex:3] retain];

    NSLog(@"\nbody:%@\nradius:%@\ngravParam:%@\neccen:%@\n",
        bodyName, meanRadius, gravParameter, eccentricity);
}


_______________________________________________

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


  • Follow-Ups:
    • Re: NSScanner Sanity Check
      • From: mmalc crawford <email@hidden>
    • Re: NSScanner Sanity Check
      • From: "Adam R. Maxwell" <email@hidden>
    • Re: NSScanner Sanity Check
      • From: Robert Martin <email@hidden>
  • Prev by Date: Re: How to convert pixels to centimeters in Cocoa
  • Next by Date: Re: NSScanner Sanity Check
  • Previous by thread: Drop image on NSImageView in panel
  • Next by thread: Re: NSScanner Sanity Check
  • Index(es):
    • Date
    • Thread