【问题标题】:How to get an array with objects to a model in Xcode from Firestore如何从 Firestore 获取包含对象的数组到 Xcode 中的模型
【发布时间】:2018-09-09 15:41:19
【问题描述】:

我有一个这样的对象数组存储在 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

我现在如何将单词添加到我的单词模型中? 我自己的猜测是我应该改变我在练习模型中保存单词数组的方式,但我不知道该怎么做。

谢谢!

【问题讨论】:

  • 试过 Array ???
  • @Alexander Singh 您在哪里检索翻译和单词字段,我们需要为这些模型类创建单独的 swift 文件,我们将这些模型类称为哪里

标签: ios swift xcode firebase google-cloud-firestore


【解决方案1】:

尝试这样做:

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

【讨论】:

  • 我们需要写入的位置以及获取 Firestore 数据的位置。
猜你喜欢
  • 2021-02-07
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2022-10-22
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多