【问题标题】:Expression resolves to an unused i-value [duplicate]表达式解析为未使用的 i 值 [重复]
【发布时间】:2016-06-07 04:47:31
【问题描述】:

我正在从数据库中检索一些数据并将它们附加到一个数组中。然后,(检索完成后),我重新加载数据并将这些数据放入 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
    }   
}

【问题讨论】:

  • 所以删除该行。它什么也没做,这就是警告消息告诉你的。
  • 我需要访问数组中的值
  • 您正在访问它但没有使用它。
  • @matt Ye,这就是原因。非常感谢
  • 其实是一个未使用的左值(也就是小写L的左值,就像利马一样)。

标签: ios arrays swift uitableview uibutton


【解决方案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

此行会引发警告,因为您只是访问该值而从未在您的应用程序中打印或使用它,从而使其未使用值访问和警告。

【讨论】:

  • Expression resolves to an unused i-value 仍然存在,即使我将其更改为。这不是问题的原因
  • 哦,好吧!谢谢!
猜你喜欢
  • 2016-04-22
  • 2017-05-30
  • 1970-01-01
  • 2015-07-16
  • 2015-06-15
  • 2015-01-29
  • 2018-04-29
  • 1970-01-01
  • 2015-12-22
相关资源
最近更新 更多