无法转换'UILabel'类型的值!预期参数'type inout String' [英] cannot convert value of type 'UILabel!' to expected argument 'type inout String'
问题描述
当我尝试增加 currentNumberAdmin
时,我得到:
When I try to increase currentNumberAdmin
I get:
无法转换价值类型'UILabel!'到期望参数'输入字符串'
cannot convert value of type '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)
})
}
}
有谁知道如何转换和增加 currentNumberAdmin
是否正确?
Does anyone know how I can convert and increase currentNumberAdmin
correctly?
推荐答案
由于这一行而崩溃: currentNumberAdmin + = String(1)
。您正在尝试将字符串值添加到UILabel值,该值无效。你实际上告诉编译器将 currentNumberAdmin
(一个UILabel)分配给将一个UILabel添加到String的表达式的值,编译器不知道如何do,因此是异常消息。
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.
为什么你试图设置标签的文本两次并不完全清楚:一次使用snapshot.value,然后再次使用线。如果您要做的是将标签的文本设置为快照值+ 1,请执行以下操作:
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:
@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)
})
}
这篇关于无法转换'UILabel'类型的值!预期参数'type inout String'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!