2016-09-19 136 views
1

我在Swift 3和Firebase中遇到了一些問題。 我的錯誤是Thread 1 Signal SIGABRT. 我的錯誤是這樣的:設置值Firebase - Swift 3

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.任何想法? 預先感謝您

回答

1

這似乎當你有選配在你的字典,嘗試宣告您的變量作爲String一個空字符串,而不是String!的初始值,這樣

var book_key = "", book_title = "", book_authors = "", book_publisher = "", number_pages = "", book_price = "", book_urlPicture1 = "", book_urlPicture2 = "", book_condition = "" 
0
在迅速3發生

感謝您的幫助。我找到了解決方案。 現在我使用的是這樣的:

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] 
+0

是的,這也適用,但要小心,如果任何這些變量是零你的程序將在該行 –

+0

這是最好的崩潰在大多數情況下避免隱式解包可選。 –