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];
}
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