简体   繁体   English

无法转换'UILabel!'类型的值! 预期参数'type inout String'

[英]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.

相关问题 swift无法将类型“ [Liquor]”的值转换为预期的参数类型“ inout _” - swift Cannot convert value of type '[Liquor]' to expected argument type 'inout _' 无法将&#39;Int&#39;类型的值转换为预期的参数类型&#39;(inout UnsafeMutableBufferPointer &lt;_&gt;,inout Int)throws - &gt; Void&#39; - Cannot convert value of type 'Int' to expected argument type '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void' 无法转换“inout NSNumber”类型的值? 到预期的参数类型 &#39;AutoreleasingUnsafeMutablePointer<AnyObject?> &#39; 错误 - Cannot convert value of type 'inout NSNumber?' to expected argument type 'AutoreleasingUnsafeMutablePointer<AnyObject?>' error 无法将类型&#39;String?.Type&#39;的值转换为预期的参数类型&#39;String?&#39; - Cannot convert value of type 'String?.Type' to expected argument type 'String?' 无法将类型(“字符串:字符串”)的值转换为预期的参数类型“ URL” - Cannot convert value of type ('string: String)' to expected argument type 'URL' 无法将类型“ [String:Any]”的值转换为预期的参数类型“ String” - Cannot convert value of type '[String : Any]' to expected argument type 'String' 无法将“字符串”类型的值转换为预期的参数类型“布尔” - Cannot convert value of type 'String' to expected argument type 'Bool' 无法将类型“ Int”的值转换为预期的参数类型“ String”? - Cannot convert value of type 'Int' to expected argument type 'String'? 无法将“String”类型的值转换为预期的参数类型“[Any]” - Cannot convert value of type 'String' to expected argument type '[Any]' 无法转换&#39;NSString&#39;类型的值? 预期参数类型&#39;String&#39; - Cannot convert value of type 'NSString'? to expected argument type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM