我的 NSMutableArray
中有 4 个元素。我有用于下载文件和在 UITextView
中显示文件数据以用于测试目的的简洁代码。没有 for
循环,一切都很好。给我问题的代码在这个函数中:
- (void)complexDownload {
int i;
for (i=0; i < downloadArray.count; i++) {
if (isBusy == NO) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
downloadURL = [downloadArray objectAtIndex:i];
NSLog(@"URL is %@", downloadURL);
NSLog(@"Downloading object at index %i", i);
NSURL *url = downloadURL;
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
if (theConnection) {
self.downloadData = [NSMutableData data];
isBusy = YES;
NSLog(@"Busy value in download cycle equals %i, downloading", isBusy);
} else {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Connection failed");
isBusy = NO;
}
}
}
}
我最初认为问题可能出在 isBusy
BOOL
中,但即使没有 if
条件,应用程序也会崩溃。编译器没有给我任何错误,但有一个错误:
Here's the link for the big screenshot .
其余函数如下:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[downloadData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *dataString = [[NSString alloc] initWithData:downloadData encoding:NSASCIIStringEncoding];
self.dataTextView.text = dataString;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Download finished!");
isBusy = NO;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%@", error);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
所有的 NSLogged 值都很好,Array 有链接并且所有链接都是正确的。
最佳答案
我的猜测是,在某些时候 downloadArray[i]
在某些时候损坏了,或者它不是 NSUrl。代码在 [NSURLRequest requestWithURL...]
调用的 CFURLCopyAbsoluteURL()
中崩溃。
关于iphone - 从 NSMutableArray 下载时“for”循环崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13954513/