【问题标题】:Setting Value Firebase - Swift 3设置值 Firebase - Swift 3
【发布时间】:2016-09-19 06:56:58
【问题描述】:

我在使用 Swift 3 和 Firebase 时遇到了一些问题。 我的错误是Thread 1 Signal SIGABRT. 我的错误是这样的:

Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(setValue:) Cannot store object of type _SwiftValue at book_key. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'

我的代码是这样的:

var book_key, book_title, book_authors, book_publisher, number_pages, book_price, book_urlPicture1, book_urlPicture2, book_condition: String!

 var ref = FIRDatabase.database().reference().child("Books").child("User's books")
        book_key = ref.childByAutoId().key


let valuesBook: NSDictionary = ["book_key": book_key, "book_title": book_title, "book_author": book_authors, "book_price": book_price, "book_publisher": book_publisher, "page_count": number_pages, "book_description": "", "book_urlImage": book_urlPicture1, "book_urlImage2": book_urlPicture2, "book_condition": book_condition]

 ref.child(book_key).setValue(valuesBook)

如果我在我的应用程序中打印我的 valuesBook,结果是绝对正确的。像这样:

{
"book_author" = Gg;
"book_condition" = "Very Good";
"book_description" = "";
"book_key" = "-KS0DIr6cQkI0oxsgwPU";
"book_price" = 3;
"book_publisher" = Jj;
"book_title" = Gg;
"book_urlImage" = "https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/book_key%2F-KS0DIr6cQkI0oxsgwPUfirstImage.jpg?alt=media&token=231324bd-5b3a-4e3e-935f-2f1177da404a";
"book_urlImage2" = "https://firebasestorage.googleapis.com/v0/b/loginsample-myapp.appspot.com/o/book_key%2F-KS0DIr6cQkI0oxsgwPUsecondImage.jpg?alt=media&token=cc207ba4-46a6-4552-a65c-13ef7c4df5ee";
"page_count" = 5;

}

结果保存在我的数据库中,但应用程序崩溃了。 在swift 3之前我没有任何错误。有什么想法吗? 提前谢谢你

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database swift3


    【解决方案1】:

    当您的字典中有 Optionals 时,这似乎发生在 swift 3 中,尝试将变量声明为 String,初始值为空字符串而不是 String!,像这样

    var book_key = "", book_title = "", book_authors = "", book_publisher = "", number_pages = "", book_price = "", book_urlPicture1 = "", book_urlPicture2 = "", book_condition = ""
    

    【讨论】:

      【解决方案2】:

      感谢您的帮助。我找到了解决方案。 现在我正在使用这个:

      let valuesBook = ["book_key": book_key, "book_title": book_title, "book_author": book_authors, "book_price": book_price, "book_publisher": book_publisher, "page_count": number_pages, "book_description": "", "book_urlImage": book_urlPicture1, "book_urlImage2": book_urlPicture2, "book_condition": book_condition] as [String: Any]
      

      而不是这个:

      let valuesBook: NSDictionary = ["book_key": book_key, "book_title": book_title, "book_author": book_authors, "book_price": book_price, "book_publisher": book_publisher, "page_count": number_pages, "book_description": "", "book_urlImage": book_urlPicture1, "book_urlImage2": book_urlPicture2, "book_condition": book_condition]
      

      【讨论】:

      • 是的,这也有效,但要小心,如果这些变量中的任何一个为 nil,您的程序将在这一行崩溃
      • 在大多数情况下最好避免隐式展开可选。
      猜你喜欢
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2017-01-30
      相关资源
      最近更新 更多