程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> cocoa touch-NSMutableArray下載時for循環崩潰

cocoa touch-NSMutableArray下載時for循環崩潰

編輯:編程綜合問答
NSMutableArray下載時for循環崩潰

在我的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()時崩潰的。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved