Erkam KUCET
Erkam KUCET

Reputation: 514

Swift use prepareForSegue in indexPath

i have a model class and two tableView Controller. This class in Integer value. This class in value send to other class with prepareforsegue method. But i can not prepareforsegue get tableView indexpath. i' m doing wrong?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let subCategory: SubCategoryVC = segue.destinationViewController as SubCategoryVC

    let indexPath = self.categoryTableView.indexPathForSelectedRow()

    model = arrList[indexPath?.row] **//incorrect line**

    subCategory.getCatID = model.categoryID
}

Upvotes: 1

Views: 5699

Answers (2)

Erkam KUCET
Erkam KUCET

Reputation: 514

yes. i found it. This problem is optional. :(

let indexPath = self.categoryTableView.indexPathForSelectedRow()**!**

I put the exclamation point on the end of the line and correct.

safe method;

if let indexPath = categoryTableView.indexPathForSelectedRow() {
    // do the work here
}

Upvotes: 2

mustafa
mustafa

Reputation: 15464

indexPath?.row returns optional value. You can't use it in array subacript. You have to uwrap it first. Here is example:

if let indexPath = categoryTableView.indexPathForSelectedRow() {
    // do the work here
}

Upvotes: 11

Related Questions