表达式解析为未使用的i值

时间:2016-06-07 04:47:32

标签: ios arrays swift uitableview uibutton

我正在从数据库中检索一些数据并将它们附加到数组中。然后,(检索完成后),我重新加载数据并将这些数据放在TableView中。在我的动态TableView中,我有按钮,所以在cellForRowAtIndexPath中,我在那里声明了它。

我检查并确保检索到的数据(如此数组)不为空。但是,按钮的选择器会抱怨

  

表达式解析为未使用的i值

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

1 个答案:

答案 0 :(得分:1)

#selector使用了错误的语法。

使用此:

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

编辑:

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

此行正在抛出警告,因为您只是访问该值并且从不在您的应用程序中打印或使用它,从而使其未使用访问值和警告。