새로운 iPhone 응용 프로그램을 개발 중입니다. 여기에서 로컬의 'csv'파일을 구문 분석하고 그 파일과 관련된 작업을 수행합니다. 프로그래밍 방식으로 서버에서 'csv'파일을 다운로드하려고하면 나에게 적합하지 않습니다. 저 좀 도와 주 시겠어요?Objective-C의 서버에서 CSV 파일을 다운로드하는 방법
-(void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
: 로컬 파일에서
데이터로드가
- (void)viewDidLoad
{
[super viewDidLoad];
NSString * file = [[NSBundle bundleForClass:[self class]] pathForResource:@"sample" ofType:@"csv"];
NSStringEncoding encoding = 0;
NSString * csv = [NSString stringWithContentsOfFile:file usedEncoding:&encoding error:nil];
NSArray *fields = [csv CSVComponents];
NSLog(@"fields: %@", fields); //getting the result content
}
는 서버에서 파일을 다운로드 (제대로 작동)
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading"); //nothing showing here
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullName = [NSString stringWithFormat:@"quotes.csv"];
NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@",docDir,fullName];
[receivedData writeToFile:fullFilePath atomically:YES];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"data: %@", data); //nothing showing here
if (receivedData)
[receivedData appendData:data];
else
receivedData = [[NSMutableData alloc] initWithData:data];
}
- (void)loadDatafromURL
{
NSURL *url = [NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC,^IXIC,^dji,^GSPC,^BVSP,^GSPTSE,^FTSE,^GDAXI,^FCHI,^STOXX50E,^AEX,^IBEX,^SSMI,^N225,^AXJO,^HSI,^NSEI&f=sl1d1t1c1ohgv&e=.csv"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
당신의 오류가 무엇입니까 :
실제로 동작하는 솔루션은 다음과 같은 비트를 보일 것이다? –
데이터가 내부에 들어오지 않습니다. - "connection didReceiveData" – shebi
그 CSVComponents 메소드는 무엇입니까? –