在我的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;
}
}
}
}
我最初以為是isBusyBOOL的問題,但是去掉if條件之後還是會崩潰。
余下的函數如下:
- (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()
時崩潰的。