2016-08-25 69 views
0

我得到错误索引超出范围数组指数超出范围阵列迅速的

这里是我的代码

.response { request, response, _, error in 
      self.localPath = destination(NSURL(string: "")!, response!) 
      self.localPathArray.append(self.localPath!) 
     } 
     cell.progressDownload.hidden = false 
     cell.progressLabel.hidden = false 
    } 

    if statusInstantiante == true { 
     let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc:RedirectMagazineViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("NEXT") as! RedirectMagazineViewController 

     vc.receiveData = self.localPathArray[indexPath.row] //Error 
     vc.receiveTitle = titleMagazine[indexPath.item] 

     self.navigationController?.pushViewController(vc, animated: true) 
    } else { 
     print("still downloading") 
    } 
} 

我使用alamofire下载的PDF文件,并获得路径(localPath来),并追加它到localPathArray。构建成功,并可以完全下载,但如果我想查看PDF文件,它打印索引超出范围。

+0

好这个问题,最有可能,是'indexPath.row',我猜你设定的行数错误值。 – 4oby

+0

啊,是的,我检查它,下载后的索引路径是不正确的,但一些PDF文件具有正确的索引路径,以便它可以查看。有什么方法可以解决它? –

回答

1

只是包装你的线弄成这个样子:

if (self.localPathArray.count > indexPath.row) { 
    //this condition ensures that your will not request an index that does not exist in the array 
    vc.receiveData = self.localPathArray[indexPath.row] 
} 
+0

在'self.webView.loadRequest(NSURLRequest(URL:receiveData))中出现新的错误'致命错误:意外地发现了零,同时展开一个可选值'' –

+0

好吧,这样就解决了。这是你最后的评论是另一个问题,这已经是一个新的问题。 – pedrouan

+0

好的,我提出了新的问题,谢谢 –