ios - 如何使用 Firestore 中的对象将数据设置为数组

标签 ios arrays swift firebase google-cloud-firestore

我想要做的是将所有数据设置到 Firestore 中的数组中,它看起来像这样: Firestore

但是我所做的给了我这个错误:

*** Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: _SwiftValue'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010b8d71e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010af6c031 objc_exception_throw + 48
    2   test collectionview                 0x00000001092e7dda -[FSTUserDataConverter parseScalarValue:context:] + 2169
    3   test collectionview                 0x00000001092e729c -[FSTUserDataConverter parseData:context:] + 792
    4   test collectionview                 0x00000001092e735b __42-[FSTUserDataConverter parseData:context:]_block_invoke + 94
    5   CoreFoundation                      0x000000010b87ac50 __NSArrayEnumerate + 592
    6   test collectionview                 0x00000001092e7122 -[FSTUserDataConverter parseData:context:] + 414
    7   test collectionview                 0x00000001092e65e3 __46-[FSTUserDataConverter parsedSetData:options:]_block_invoke + 346
    8   libswiftCore.dylib                  0x000000010e8a0af1 _T0s26_SwiftDeferredNSDictionaryC23enumerateKeysAndObjectsySi7options_ys9UnmanagedVyyXlG_AGSpys5UInt8VGtXB5usingtFTf4dgn_n + 289
    9   libswiftCore.dylib                  0x000000010e703b11 _T0s26_SwiftDeferredNSDictionaryC23enumerateKeysAndObjectsySi7options_ys9UnmanagedVyyXlG_AGSpys5UInt8VGtXB5usingtF + 17
    10  libswiftCore.dylib                  0x000000010e7040cb _T0s26_SwiftDeferredNSDictionaryC23enumerateKeysAndObjectsySi7options_ys9UnmanagedVyyXlG_AGSpys5UInt8VGtXB5usingtFTo + 43
    11  test collectionview                 0x00000001092e6303 -[FSTUserDataConverter parsedSetData:options:] + 467
    12  test collectionview                 0x00000001092dc0aa -[FIRDocumentReference setData:options:completion:] + 147
    13  test collectionview                 0x00000001092dbf1a -[FIRDocumentReference setData:] + 92
    14  test collectionview                 0x00000001091e8f85 _T019test_collectionview25NewExerciseViewControllerC08doneWithD6ActionySo15UIBarButtonItemCF + 7845
    15  test collectionview                 0x00000001091e91cc _T019test_collectionview25NewExerciseViewControllerC08doneWithD6ActionySo15UIBarButtonItemCFTo + 60
    16  UIKit                               0x000000010c46a3e8 -[UIApplication sendAction:to:from:forEvent:] + 83
    17  UIKit                               0x000000010ce6f209 __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 154
    18  UIKit                               0x000000010ce6f142 -[_UIButtonBarTargetAction _invoke:forEvent:] + 154
    19  UIKit                               0x000000010c46a3e8 -[UIApplication sendAction:to:from:forEvent:] + 83
    20  UIKit                               0x000000010c5e57a4 -[UIControl sendAction:to:forEvent:] + 67
    21  UIKit                               0x000000010c5e5ac1 -[UIControl _sendActionsForEvents:withEvent:] + 450
    22  UIKit                               0x000000010c5e4a09 -[UIControl touchesEnded:withEvent:] + 580
    23  UIKit                               0x000000010c4df0bf -[UIWindow _sendTouchesForEvent:] + 2729
    24  UIKit                               0x000000010c4e07c1 -[UIWindow sendEvent:] + 4086
    25  UIKit                               0x000000010c484310 -[UIApplication sendEvent:] + 352
    26  UIKit                               0x000000010cdc56af __dispatchPreprocessedEventFromEventQueue + 2796
    27  UIKit                               0x000000010cdc82c4 __handleEventQueueInternal + 5949
    28  CoreFoundation                      0x000000010b879bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    29  CoreFoundation                      0x000000010b85e4af __CFRunLoopDoSources0 + 271
    30  CoreFoundation                      0x000000010b85da6f __CFRunLoopRun + 1263
    31  CoreFoundation                      0x000000010b85d30b CFRunLoopRunSpecific + 635
    32  GraphicsServices                    0x000000011147aa73 GSEventRunModal + 62
    33  UIKit                               0x000000010c469057 UIApplicationMain + 159
    34  test collectionview                 0x00000001091f19a7 main + 55
    35  libdyld.dylib                       0x000000010f1c6955 start + 1
    36  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

我为我的“练习”制作了一个模型,如下所示:

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 : DocumentSerializable {
    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)
    }
}

我已经尝试使用此代码来设置数据,它确实适用于除单词部分之外的所有数据:

let newExercise = Exercise(title: exerciseTitleTxt.text!, language: language1Txt.title(for: .normal)!, translated: language2Txt.title(for: .normal)!, uid: "test", userId: uid!, dueDate: dueDateMiliseconds, lastOpen: Date().millisecondsSince1970, words: wordsArray)

       db.collection("exercises").document("test2").setData(newExercise.dictionary)

为了将单词放入“wordsArray”中,我这样做:

let word = Word(translation: translationTxt.text!, word: wordTxt.text!)
wordsArray.append(word)

所以我自己的猜测是,我需要改变在练习模型中保存单词的方式,但我无法完全理解我到底要如何做到这一点。

感谢您提前提供的帮助!

最佳答案

这是因为你的words参数是 Word 对象的数组 - Array<Word>[Word] 。 Firebase 不知道如何序列化它。您需要做的是将单词数组转换为字典数组。

var dictionary:[String:Any] {
      return [
          ...
          "words" : words.map({$0.dictionary})
      ]
}

关于ios - 如何使用 Firestore 中的对象将数据设置为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51951473/

相关文章:

python - 连接两个一维到两列数组

swift - session 开始时的 Flurry 监听器

ios - NSBlock 的访问限定符是复制还是弱还是强?

ios - 动态更改 View Swift 的框架

javascript - 如何在 map 迭代中获取特定对象键

javascript - 将数组转换为包含对象

ios - 此类与键 XXX 的键值编码不兼容 - 类是否正确连接?

ios - 我应该在这里使用 awakeFromNib 还是 initWithCoder?

swift - 如何在 SpriteKit (Swift) 中获取施加在对象上的力

ios - 获取所需大小的图标