• 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: Robert Martin <email@hidden>
  • Date: Tue, 8 May 2007 14:30:14 -0400

I'm not a scanner expert, but from the data format you quote, why use a scanner at all? It seems that all the data elements are separated by 'space' (maybe a tab?), so perhaps you could use [line componentsSeparatedByString:@" " or "\t"] to get the array of data in one-shot.

Then maybe parse the array into a dictionary keyed with the 'body', where the value is the array from elements 1->count....that way you'd capture all the 'extra info'


On May 8, 2007, at 1:47 PM, James Hillhouse wrote:

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:
40frontiernet.net


This email sent to email@hidden


_______________________________________________

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: NSScanner Sanity Check
  • Next by Date: How to implement a "Picture Frame" view
  • Previous by thread: NSScanner Sanity Check
  • Next by thread: Re: NSScanner Sanity Check
  • Index(es):
    • Date
    • Thread