无法转换“UILabel”类型的值!' to expected arguments 'type inout String'

时间:2022-03-18 16:33:47

When I try to increase currentNumberAdmin I get:

当我试图增加currentNumberAdmin时,我得到:

cannot convert value of type 'UILabel!' to expected argument 'type in out String'

无法转换“UILabel”类型的值!" to expected arguments "输入out String "

class adminPanel: UIViewController {

    @IBOutlet weak var currentNumberAdmin: UILabel!                       

    @IBAction func nextCurrent(_ sender: UIButton) {
        let database = FIRDatabase.database().reference()
        database.child("current").observe(FIRDataEventType.value, with: { (snapshot) in

          self.currentNumberAdmin.text = snapshot.value as! String
          currentNumberAdmin += String(1)
        })

    }
}

Does anyone know how I can convert and increase currentNumberAdmin correctly?

有人知道我如何正确地转换和增加currentNumberAdmin吗?

1 个解决方案

#1


1  

This is crashing because of this line: currentNumberAdmin += String(1). You're attempting to add a String value to a UILabel value, which is invalid. You're literally telling the compiler to assign currentNumberAdmin, a UILabel, to the value of the expression of adding a UILabel to a String, which the compiler doesn't know how to do, hence the exception message.

这是因为这一行:currentNumberAdmin += String(1)而导致的崩溃。您试图向UILabel值添加一个字符串值,该值无效。实际上,您是在告诉编译器将currentNumberAdmin(一个UILabel)分配给将UILabel添加到一个字符串的表达式的值,编译器不知道该如何做,因此有一个异常消息。

It's not entirely clear why you're attempt to set the label's text twice: once with snapshot.value, and then again on the next line. If what you're trying to do is set the label's text to the snapshot value + 1, do something like this:

不完全清楚为什么要设置标签的文本两次:一次是快照。值,然后在下一行。如果您要做的是将标签的文本设置为快照值+ 1,请执行以下操作:

@IBAction func nextCurrent(_ sender: UIButton) {
    let database = FIRDatabase.database().reference()
    database.child("current").observe(FIRDataEventType.value, with: { (snapshot) in

      var strVal = Int(self.currentNumberAdmin.text)!
      strVal += 1
      self.currentNumberAdmin.text = String(strVal)
    })

}

#1


1  

This is crashing because of this line: currentNumberAdmin += String(1). You're attempting to add a String value to a UILabel value, which is invalid. You're literally telling the compiler to assign currentNumberAdmin, a UILabel, to the value of the expression of adding a UILabel to a String, which the compiler doesn't know how to do, hence the exception message.

这是因为这一行:currentNumberAdmin += String(1)而导致的崩溃。您试图向UILabel值添加一个字符串值,该值无效。实际上,您是在告诉编译器将currentNumberAdmin(一个UILabel)分配给将UILabel添加到一个字符串的表达式的值,编译器不知道该如何做,因此有一个异常消息。

It's not entirely clear why you're attempt to set the label's text twice: once with snapshot.value, and then again on the next line. If what you're trying to do is set the label's text to the snapshot value + 1, do something like this:

不完全清楚为什么要设置标签的文本两次:一次是快照。值,然后在下一行。如果您要做的是将标签的文本设置为快照值+ 1,请执行以下操作:

@IBAction func nextCurrent(_ sender: UIButton) {
    let database = FIRDatabase.database().reference()
    database.child("current").observe(FIRDataEventType.value, with: { (snapshot) in

      var strVal = Int(self.currentNumberAdmin.text)!
      strVal += 1
      self.currentNumberAdmin.text = String(strVal)
    })

}