2 bluetooth99 bluetooth99 于 2012.12.20 10:32 提问

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中的数组链接也正确。

编译屏幕截图点击这里

2个回答

g989_1314125
g989_1314125   2012.12.20 13:37
已采纳

我猜测应该某些点的downloadArray[i]出了问题,或者因为它不是NSUrl。代码是在[NSURLRequest requestWithURL...]调用CFURLCopyAbsoluteURL()时崩溃的。

jiangyuaiqing
jiangyuaiqing   2012.12.20 13:59

如果你上一下出错提示,那就好办了。

bluetooth99
bluetooth99 麻烦你看一下屏幕截图吧
5 年多之前 回复
suannai0314
suannai0314 额,这个应该算评论?建议看一下这个哦http://ask.csdn.net/pages/how_to_answer评论的内容可以用评论功能哦。
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!