2011-03-21 6 views
0

J'ai un fichier CSV avec quatre champs, "Woonplaats", "Gemeente", "Provincie", "Latitude" et "Longitude".NSScanner et un fichier CSV

valeurs Exemple:

Graveland, Wijdemeren, Noord-Holland, 52.24412000,5.12150000

En utilisant le code ci-dessous, je reçois la chaîne dans mon texte, et je veux l'enregistrer dans le tableau. Comment dois-je utiliser NSScanner pour obtenir des données de cette chaîne et enregistrer dans un tableau contenant des dictionnaires?

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"csv"]; 
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ]; 
NSScanner *scanner = [NSScanner scannerWithString:myText]; 
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n ,"]]; 
NSMutableArray *newPoints = [NSMutableArray array];   

Répondre

1

Je crois que c'est ce que vous cherchez. J'ai utilisé le forum Dan Wood et l'ai modifié pour votre besoin.

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) 
{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    // insert code here... 

    NSString *path [email protected]"cities.csv"; 
    NSError *error; 
    NSString *stringFromFileAtPath = [[NSString alloc] 
             initWithContentsOfFile:path 
             encoding:NSUTF8StringEncoding 
             error:&error]; 

    NSMutableDictionary *lineDict = [NSMutableDictionary dictionary]; 
    NSArray *lines = [stringFromFileAtPath componentsSeparatedByString:@"\n"]; 
    NSEnumerator*theEnum = [lines objectEnumerator]; 
    NSArray *keys = nil; 
    int keyCount = 0; 
    NSString *theLine; 

    while (nil != (theLine = [theEnum nextObject])) 
    { 
     if (![theLine isEqualToString:@""] && ![theLine hasPrefix:@"#"]) // ignore empty lines and lines that start with # 
     { 
      if (nil == keys) // Is keys not set yet? If so, process first real line as list of keys 
      { 
       keys = [theLine componentsSeparatedByString:@","]; 
       keyCount = [keys count]; 
      } 
      else // A data line 
      { 
       NSArray *values = [theLine componentsSeparatedByString:@","]; 
       int valueCount = [values count]; 
       int i; 

       for (i = 0 ; i < keyCount && i < valueCount ; i++) 
       { 
        NSString *value = [values objectAtIndex:i]; 
        if (nil != value && ![value isEqualToString:@""]) 
        { 
         [lineDict setObject:value forKey:[keys objectAtIndex:i]]; 
        } 
       } 
      } 
     } 
    } 

    for (id key in lineDict) 
    { 
     NSLog(@"key: %@, value: %@", key, [lineDict objectForKey:key]); 
    } 

    [pool drain]; 
    return 0; 
} 

Et la sortie est:

2011-07-13 20:02:41.898 cities[5964:903] key: Latitude, value: 52.24412000 
2011-07-13 20:02:41.900 cities[5964:903] key: Provincie, value: Noord-Holland 
2011-07-13 20:02:41.900 cities[5964:903] key: Longitude, value: 5.12150000 
2011-07-13 20:02:41.901 cities[5964:903] key: Gemeente, value: Wijdemeren 
2011-07-13 20:02:41.902 cities[5964:903] key: Woonplaats, value: Graveland 
Questions connexes