0

i get error index out of range in array

here's my code

.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")
    }
}

I Download pdf file using alamofire download, and get the path (localPath) and append it to localPathArray. the build succeded and can download completely but if i want to view the pdf file it prints index out of range.

2
  • Well the problem, most probably, is with indexPath.row, I guess you set the wrong value for number of rows. Commented Aug 25, 2016 at 8:53
  • ah, yes i check it and after download the index path is not correct, but some of pdf file have the correct index path so it can view. is there any way to solve it? Commented Aug 25, 2016 at 9:01

1 Answer 1

1

Just wrap your line into something like this:

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]
}
Sign up to request clarification or add additional context in comments.

2 Comments

appear new error fatal error: unexpectedly found nil while unwrapping an Optional value in self.webView.loadRequest(NSURLRequest(URL: receiveData))
Ok so this was solved. And this what you last worte to comment is another problem, that is already for a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.