senty senty - 6 months ago 17
Swift Question

Expression resolves to an unused i-value

I am retrieving some data from database and appending them in an array. Then, (after retrieving completed), I reload data and put those data in TableView. In my dynamic TableView, I have buttons, so in

cellForRowAtIndexPath
, I declare it there.

I check and make sure that the retrieved data (so array) is not empty. However, the button's selector complains


Expression resolves to an unused i-value


var users = [String]()

// First I store in users array and reload tableView

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MyTableViewCell

cell.myBtn.tag = indexPath.row
cell.myBtn.addTarget(self, action: #selector(ViewController.btnClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)

return cell
}

func btnClicked (sender:UIButton) {
let btnRow = sender.tag
if !self.users.isEmpty {
self.users[btnRow] // This line complains: Expression resolves to an unused i-value
}
}

Answer

you are using wrong syntax for #selector. Use this

   cell.myBtn.addTarget(self, action: #selector(ViewController.btnClicked(_:), forControlEvents: UIControlEvents.TouchUpInside)

Edit :

    self.users[btnRow]  // This line complains: Expression resolves to an unused i-value

This line is throwing the warning since you just access the value and never print or use it inside your app thus making it unused accessing of value and the warning.