如何从Firestore中获取带有Xcode模型对象的数组

时间:2018-03-30 15:30:44

标签: 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

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

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试这样做:

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的结构并将其用于此问题。