[英]cannot convert value of type 'UILabel!' to expected argument 'type inout String'
When I try to increase currentNumberAdmin
I get: 当我尝试增加
currentNumberAdmin
我得到:
cannot convert value of type 'UILabel!'
无法转换'UILabel!'类型的值! to expected argument 'type in 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
吗?
This is crashing because of this line: currentNumberAdmin += String(1)
. 由于以下行而导致崩溃:
currentNumberAdmin += String(1)
。 You're attempting to add a String value to a UILabel value, which is invalid. 您正在尝试将字符串值添加到UILabel值,这是无效的。 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. 您实际上是在告诉编译器将UILabel的
currentNumberAdmin
分配给向字符串添加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. 尚不清楚为什么您要尝试两次设置标签的文本:一次是使用snapshot.value,然后是下一行。 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)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.