3
我使用下面顯示的代碼下載了該文件。然後,我試圖將NSMutableData變量保存到文件,但是,該文件未創建。我究竟做錯了什麼?我是否需要將NSMutableData轉換爲NSString?NSMutableData保存到文件
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)_response {
response = [_response retain];
if([response expectedContentLength] < 1) {
data = [[NSMutableData alloc] init];
}
else {
data = [[NSMutableData dataWithCapacity:[response expectedContentLength]] retain];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {
[data appendData:_data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];
NSLog(@"saved: %@", filePath);
[data writeToFile:filePath atomically:YES];
NSLog(@"downloaded file: %@", data); //all i see in log is some encoded data here
}
這肯定會幫助,如果你傳遞了一個NSError變量將writeToFile的地址,並測試了函數的返回值。 – 2012-02-28 21:43:07