ios - 如何从 Firestore 将包含对象的数组获取到 Xcode 中的模型

标签 ios swift xcode firebase google-cloud-firestore

我有一个像这样的对象数组存储在 firestore 中: database

我有一个整个练习的模型:

import Foundation
import Firestore

protocol  DocumentSerializable {
    init?(dictionary:[String:Any])
}


struct Exercise {
    var title: String
    var language: String
    var translated: String
    var uid: String
    var userId: String
    var dueDate: Int
    var lastOpen: Int
    var words: Array<Any>

    var dictionary:[String:Any] {
        return [
            "title":title,
            "language":language,
            "translated":translated,
            "uid":uid,
            "userId":userId,
            "dueDate":dueDate,
            "lastOpen":lastOpen,
            "words":words
        ]
    }
}

extension Exercise : DocumentSerializable {
    init?(dictionary: [String : Any]) {
        guard let title = dictionary["title"] as? String,
            let language = dictionary["language"] as? String,
            let translated = dictionary["translated"] as? String,
            let uid = dictionary["uid"] as? String,
            let userId = dictionary["userId"] as? String,
            let dueDate = dictionary["dueDate"] as? Int,
            let lastOpen = dictionary["lastOpen"] as? Int,
            let words = dictionary["words"] as? Array<Any>

            else {return nil}

        self.init(title: title, language: language, translated: translated, uid: uid, userId: userId, dueDate: dueDate, lastOpen: lastOpen, words: words)
    }
}

然后我想将这些单词传递给这样的模型:

import Foundation
import Firestore

protocol  DocumentSerializable2 {
    init?(dictionary:[String:Any])
}

struct Word {
    var translation: String
    var word: String



    var dictionary:[String:Any] {
        return [
            "translation":translation,
            "word":word

        ]
    }
}

extension Word : DocumentSerializable2 {
    init?(dictionary: [String : Any]) {
        guard let translation = dictionary["translation"] as? String,
            let word = dictionary["word"] as? String
            else {return nil}

        self.init(translation: translation, word: word)
    }


}

然后我可以使用此函数检索数据:

func loadData() {
        docRef = db.collection("exercises").document(exerciseId)

        docRef.getDocument{ (docSnapshot, error) in
            guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
            let data = docSnapshot.data()
            self.exerciseArray = docSnapshot.data().flatMap({_ in Exercise(dictionary: data)})
            let exercise = self.exerciseArray[0]

            print(exercise.words)

            self.language1Label.text = exercise.language
            self.translateLabel.text = exercise.translated
            self.navigationItem.title = exercise.title
        }
    } 

我可以将单词获取到对象数组中,打印时该数组如下所示: printed array

现在如何将单词添加到我的单词模型中? 我自己的猜测是,我应该更改在练习模型中保存单词数组的方式,但我无法理解如何做到这一点。

谢谢!

最佳答案

尝试这样做:

struct Word {
    var translation: String
    var word: String

    var dictionary:[String:Any] {
        return [
            "translation":translation,
            "word":word
        ]
    }

   // init that takes a Dictionary
   init(dictionary: [String:Any]) {
       self.translation = dictionary["translation"] as? String ?? "No Value"
       self.word = dictionary["word"] as? String ?? "No Value"
    }
}

然后您可以设置如下值:

let words = [Word]()
for record in exercise.words {
    words.append( Word(dictionary: record))
}

找到了这个here的结构并将其用于这个问题。

关于ios - 如何从 Firestore 将包含对象的数组获取到 Xcode 中的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577112/

相关文章:

ios - 获取 npm 错误!退出,错误代码为 : 128

ios - swift 4 可编码 : Converting JSON return String to Int/Date/Float

ios - 如何以编程方式使我的应用静音(禁用声音)

ios - 带有 iOS 自动签名的推送通知证书

iphone - 如何使 cocos2d Sprite 每秒缩放(以获得脉动效果)?

iOS 证书过期 : Do you resubmit app?

iphone - 如何使用 iOS GPUImage 生成直方图?

swift - 发件人 : UIImageView ! == CollectionViewCell 中的 UIImageView

swift - 如何连续发射子弹

ios - 即使在小代码示例中将 unowned 与 Protocol 一起使用时也会发生 Swift Leak