Ariel Gemilang
Ariel Gemilang

Reputation: 791

index out of range array swift

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.

Upvotes: 0

Views: 357

Answers (1)

pedrouan
pedrouan

Reputation: 12910

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]
}

Upvotes: 1

Related Questions