索引超出范围数组swift

时间:2016-08-25 08:50:05

标签: arrays swift

我的错误索引超出了数组

的范围

这是我的代码

.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文件,它会打印索引超出范围。

1 个答案:

答案 0 :(得分: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]
}