0
我有一個csv文件,它存儲3個值 - 城市名稱以及州,經度和緯度值。將csv文件中的數據加載到xcode時,NSString不能正確顯示
,當我用Excel打開會出現採樣數據:馬薩諸塞州波士頓,411.932,73
,我面對的第一個問題,當我CSV文件複製到Xcode項目文件夾時,Xcode將文件數據顯示爲「Albany,NY」,397.898,73,將「」添加到第一列數據。
,我面對的是,當我取回從CSV文件中的數據,並造成城市名作爲的NSString,該字符串只顯示奧爾巴尼代替奧爾巴尼,紐約
這是我做的第二個問題。感謝您的幫助。
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];
}
它似乎在textedit中也有引號,但不在excel中。這很奇怪。而對於arr,如果我的第一個數組包含「Albany,N.Y.」,這是否意味着當我解析第一部分時,他們將會在「Albany」之後跳過N.Y.並僅將Albany解析爲NSString? – Jin 2011-01-27 00:10:30