2011-01-26 5 views
0

J'ai un fichier csv qui stocke 3 valeurs - un nom de ville avec l'état, les longitudes et les latitudes.NSString ne s'affiche pas correctement lors du chargement des données du fichier csv à xcode

A données d'échantillon comme apparaît lorsque je l'ouvre avec Excel serait: Boston, Mass., 411,932, 73

Le premier problème que je fais face est que lorsque je copie le fichier .csv dans le dossier de projet Xcode, Xcode affiche les données du fichier comme "Albany, NY", 397.898,73, en ajoutant "" aux données de la première colonne.

Le deuxième problème que je fais face est que lorsque je récupère les données à partir du fichier csv et pose le nom de la ville comme NSString, la chaîne montre seulement Albany au lieu d'Albany, N.Y.

Voici ce que je faisais. Merci de votre aide.

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"citiesdataUsed" ofType:@"csv"]; 
NSString* fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 
[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 

NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 
cityList = [[NSMutableArray alloc]init]; 

//skip line 0 as it is the title of each column 
for(int idx = 0; idx < pointStrings.count; idx++) 
{ 
    Pair *sth = [[Pair alloc] init]; 
    // break the string down even further to the columns. 
    NSString* currentPointString = [pointStrings objectAtIndex:idx]; 
    NSArray* arr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

    NSString *country = [[NSString alloc] initWithFormat:@"%@", [arr objectAtIndex:0]]; 
    NSNumber *latData = [[NSNumber alloc] initWithFloat:[[arr objectAtIndex:2] floatValue]]; //long 
    NSNumber *longData = [[NSNumber alloc] initWithFloat:[[arr objectAtIndex:1] floatValue]]; //lat 

    //convert the NSNumbers to float value 
    float longData2 = [longData floatValue]; 
    float latData2 = [latData floatValue]; 
    CGPoint temp = CGPointMake(longData2,latData2); 
    sth.city = country; 
    sth.location = temp; 
    [cityList addObject: sth]; 
    [sth release]; 
} 

Répondre

0

En ce qui concerne votre pays d'analyse syntaxique que la première partie ..

Vous fractionnez la ligne par , donc votre tableau ressemblerait

arr[0] = Albany 
arr[1] = N.Y. 
arr[2] = 397.898 
arr[3] = 73 

Votre code devrait montrer que

NSString *country = [[NSString alloc] initWithFormat:@"%@, %@", [arr objectAtIndex:0], [arr objectAtIndex:1]]; 
NSNumber *latData = [[NSNumber alloc] initWithFloat:[[arr objectAtIndex:2] floatValue]]; //long 
NSNumber *longData = [[NSNumber alloc] initWithFloat:[[arr objectAtIndex:3] floatValue]]; //lat 

Vous ne savez pas trop pourquoi xcode inclut les devis dans votre fichier CSV fichier mais si les guillemets seulement affichent dans xcode et non dans (par exemple) textedit, votre fichier sera ok.

+0

il semble avoir des citations dans textedit aussi mais pas dans Excel. C'est bizarre. Et pour l'arr, si mon premier tableau contient "Albany, N.Y.", cela signifie-t-il que lorsque j'analyserai la première partie, le "," après Albany passera le N.Y. et n'analysera qu'Albany en NSString? – Jin

Questions connexes