gpt4 book ai didi

ios - 未捕获的异常“NSFileHandleOperationException”

转载 作者:行者123 更新时间:2023-12-01 16:38:34 27 4
gpt4 key购买 nike

我试图同时使用以下代码从我自己的服务器上下载几个文件:

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// create
[[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
_file = [NSFileHandle fileHandleForUpdatingAtPath:strFilePath];// read more about file handle
if (_file) {
[_file seekToEndOfFile];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
//write each data received
if( receivedata != nil){
if (_file) {
[_file seekToEndOfFile];
}
[_file writeData:receivedata];
}
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//close file after finish getting data;
[_file closeFile];
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//do something when downloading failed
}
- (IBAction)downloadFromServer:(id)sender {

NSLog(@"File now being downloaded");
while (i<=3) {

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *strFileURL =[NSURL URLWithString:[NSString stringWithFormat:@"SomePath/pic%d.png", i]];

[request setURL:strFileURL];
NSURLConnection *conection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conection start];
strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic%d.png", i]];

}

}

我有3张图片,pic1.png,pic2.png和pic3.png。现在,如果我运行此代码,该应用程序将仅保存一个名为pic3.png的损坏文件并自动崩溃。我需要下载所有三个文件,我要去哪里的任何指针?

最佳答案

很显然,这是行不通的。您开始三个异步操作。每次启动异步操作后,都将文件名存储在一个未知的位置,但可能存储在相同的位置。那么,第一个异步操作完成时将使用什么文件名?提示:您认为不会使用的那个。

关于ios - 未捕获的异常“NSFileHandleOperationException”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26213093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com