ios - 将内容从 Firebase 拉入 Swift 的问题

标签 ios swift database firebase firebase-realtime-database

enter image description here

我正试图从 Firebase 获取报价,但我很挣扎。当然,我也不知道我在做什么。我需要一些帮助!

在 Firebase 中,我的报价设置如下:

root --> quotes --> quoteID --> quoteText, quoteAttribution

我正在尝试从 Firebase 中提取引号,将它们添加到本地数组(稍后放入字典),然后随机提取一个以在应用程序中使用。我希望将quoteText放入quoteLabel.text,将quoteAttribution放入authorLabel.text。我在另一个 StackOverflow 问题中找到了这个解决方案,但它在第 43 行抛出以下错误:

Could not cast value of type 'NSNull' (0x10f549740) to 'NSDictionary' (0x10f549178). 2018-07-21 22:49:50.241473-0400 Wavefully[72475:1126119] Could not cast value of type 'NSNull' (0x10f549740) to 'NSDictionary' (0x10f549178).

对于如何从 Firebase 中提取 quoteTextquoteAttribution 以在我的应用程序中使用,是否有人有任何提示?


这是我的代码:

class ViewController: UIViewController {

class quoteClass {
    var uid = ""
    var quote = ""
    var author = ""
}

@IBOutlet weak var quoteLabel: UILabel!
@IBOutlet weak var authorLabel: UILabel!

var ref: DatabaseReference?
var databaseHandler: DatabaseHandle?

var quotesArray = [quoteClass]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the reference to Firebase
    ref = Database.database().reference()

    let quotesRef = ref?.child("quotes")
    quotesRef?.observeSingleEvent(of: .value, with: { (snapshot) in
        for _ in snapshot.children {
            let quoteSnap = snapshot
            let quoteKey = quoteSnap.key

            let thisQuoteRef = quotesRef?.child("quoteID")
            thisQuoteRef?.observeSingleEvent(of: .value, with: { (quoteSnap) in
                let singlequoteSnap = quoteSnap
                let quoteDict = singlequoteSnap.value as! [String:AnyObject]
                let quote = quoteDict["quoteText"]
                let author = quoteDict["quoteAttribution"]

                let aQuote = quoteClass()
                aQuote.uid = quoteKey
                aQuote.quote = quote as! String
                aQuote.author = author as! String

                print(aQuote.quote)
                print(aQuote.author)
                print(aQuote.uid)

                self.quotesArray.append(aQuote)
            })
        }
    })

    let singleQuote = quotesArray.randomItem()!
    print(singleQuote.uid)
    print(singleQuote.quote)
    print(singleQuote.author)}}

非常感谢您的帮助!

最佳答案

或者,您也可以通过将数据转换为 NSDictionary 来使用您的数据,如下所示:

        let dictionary = snapshot.value as? NSDictionary
        let quote = dictionary["quoteText"] as? String ?? ""

关于ios - 将内容从 Firebase 拉入 Swift 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51461658/

相关文章:

ios - UIButton 中的按钮和图像对齐问题

ios - 重用系统和以编程方式设计的 TableView 问题

ios - swift 中的 "terminating with uncaught exception of type NSException"

ios - clang : error: linker command failed with exit code 1 (use -v to see invocation) with Quickblox

sql - 仅当 ANSI sql 不存在时才创建表

sql - 数据库规范化和快速搜索

iphone - 如何获取 CALayer 的上下文加上它周围的几个像素?

android - 使溢出滚动条在 iOS 和 Android 移动设备上可见

swift - 你能在 Swift 中重载类型转换运算符吗?

java - 将Entity类转换为很多DTO