【问题标题】:Value of type 'Any?' has no member 'description'“任何?”类型的值没有成员“描述”
【发布时间】:2017-02-06 01:05:49
【问题描述】:

我是 firebase 的初学者,我只是在观看 https://www.youtube.com/watch?v=joVi3thZOqc&t=301s 上的视频,但我收到了这个错误“Value of type 'Any?'”没有成员“描述””这是我的代码:

import UIKit
import Firebase
import FirebaseDatabase

class ViewController: UIViewController {
    @IBOutlet weak var conditionLable: UILabel!

    @IBAction func sunnyDidTouch(_ sender: Any) {
    }
    @IBAction func FoggyDidTouch(_ sender: Any) {
    }

    let rootRef = FIRDatabase.database().reference()



    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let conditionRef = rootRef.child("condition")
        conditionRef.observe(FIRDataEventType.value, with: { (snapshot : FIRDataSnapshot) in

            self.conditionLable.text = snapshot.value?.discription
        })
    }

}

【问题讨论】:

  • 在标题中你说“描述”,但代码有discription(带有i)。如果这是您实际代码中的拼写错误,则可以解释问题。
  • conditionLabel 中还有一个错字...
  • 那是一个错字。你现在可以忽略这个问题我通过改变这个来解决它: let conditionRef = FIRDatabase.database().reference().child("condition") conditionRef.observe(FIRDataEventType.value, with: { (snapshot : FIRDataSnapshot) in self.conditionLable.text = (snapshot.value as AnyObject).description
  • 但是,如果有人发现我的代码有问题,所有帮助仍将不胜感激我仍然在应用程序委托中收到信号 SIGABRT。
  • @MikaelWeiss Chek conditionLable 在情节提要中正确连接。

标签: firebase swift3 firebase-realtime-database


【解决方案1】:

主要目的是获取condition键的value对吗。

所以使用snap.value 来获取它。

let conditionRef = rootRef.child("condition")

conditionRef.observe(FIRDataEventType.value, with: { (snapshot : FIRDataSnapshot) in
  let weather = snap.value as? String

  self.conditionLable.text = weather
})

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多