상태, 경도 및 위도 값과 함께 도시 이름과 같은 3 개의 값을 저장하는 CSV 파일이 있습니다.csv 파일에서 xcode로 데이터를로드 할 때 NSString이 올바르게 표시되지 않습니다.
나는 것 엑셀로 열 때 등장 샘플 데이터 :. 보스턴, 질량, 411.932, 73
I가 직면 첫 번째 문제가 내가 Xcode 프로젝트 폴더에 csv 파일을 복사, 엑스 코드 첫 번째 열 데이터에 ""을 추가하여 파일 데이터를 "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가 NS.String을 건너 뛴 다음 Albany를 NSString으로 구문 분석 한 후 첫 번째 부분을 구문 분석하면 ","이됩니다. – Jin