InvalidFirebaseData Swift 3 只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。
Posted
技术标签:
【中文标题】InvalidFirebaseData Swift 3 只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。【英文标题】:InvalidFirebaseData Swift 3 Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.' 【发布时间】:2017-10-11 10:04:45 【问题描述】:我正在尝试将 Firebase 集成到我的应用中,并希望以 以下格式存储数据。
"chits":
"Chit-1":
"Bidders": [
"bankAccNumber": "5555566966",
"bankIFSC": "GFHJJHHJ",
"bankName": "55666",
"bidderEmail": "desi@venkat.co.in",
"bidderId": "Bidder-1",
"bidderName": "Venkat Desi",
"bidderPhone": "9999999999",
"checked": false,
"paymentType": 66555
,
"bankAccNumber": "5555566966",
"bankIFSC": "GFHJJHHJ",
"bankName": "55666",
"bidderEmail": "desi@venkat.co.in",
"bidderId": "Bidder-2",
"bidderName": "Venkat Desi",
"bidderPhone": "9999999999",
"checked": false,
"paymentType": 66555
,
"bankAccNumber": "5555566966",
"bankIFSC": "GFHJJHHJ",
"bankName": "55666",
"bidderEmail": "desi@venkat.co.in",
"bidderId": "Bidder-3",
"bidderName": "Venkat Desi",
"bidderPhone": "9999999999",
"checked": false,
"paymentType": 66555
],
"chitId": "Chit-1",
"chitName": "ABC1",
"chitTotalValue": 240000,
"endDate": "11/10/2018",
"monthlyShare": 1000,
"startDate": "11/10/2017",
"totalMonths": 12
,
"Chit-2":
"bidderIds": [
"bidderId": "0",
"bidderName": "Rajesh Challuri",
"bidderPhone": "3333333333",
"checked": true,
"paymentType": 0
,
"bidderId": "0",
"bidderName": "Govinda Bhonsle",
"bidderPhone": "9999999999",
"checked": true,
"paymentType": 0
],
"chitId": "Chit-2",
"chitName": "ABC2",
"chitTotalValue": 100000,
"endDate": "12/10/2018",
"monthlyShare": 8000,
"startDate": "12/10/2017",
"totalMonths": 12
,
"Chit-3":
"bidderIds": [
"bidderId": "0",
"bidderName": "Rajesh Challuri",
"bidderPhone": "3333333333",
"checked": true,
"paymentType": 0
,
"bidderId": "0",
"bidderName": "Govinda Bhonsle",
"bidderPhone": "9999999999",
"checked": true,
"paymentType": 0
],
"chitId": "Chit-3",
"chitName": "ABC3",
"chitTotalValue": 200000,
"endDate": "13/10/2018",
"monthlyShare": 2000,
"startDate": "13/10/2017",
"totalMonths": 12
我成功地存储了投标人数据,即姓名、电子邮件、电话、帐户详细信息等。但是,我还需要存储 Chit 数据。下面是这两个类的代码。
Bidder.swift
class Bidder
let bidderId: Int
let bidderName: String
let bidderEmail: String
let bidderPhone : String
let modeOfPayment: Int
let bidderBankName: String
let bidderBankAcno: String
let bidderIfscCode: String
let paymentArray : [[String:String]]?
let chitrefArray : [String]?
let ref: FIRDatabaseReference?
init(bidderId: Int, bidderName: String,bidderEmail: String, bidderPhone: String, modeOfPayment: Int, bidderBankName: String, bidderBankAcno: String, bidderIfscCode: String,paymentArray: [[String:String]], chitrefArray: [String])
self.bidderId = bidderId
self.bidderName = bidderName
self.bidderEmail = bidderEmail
self.bidderPhone = bidderPhone
self.modeOfPayment = modeOfPayment
self.bidderBankName = bidderBankName
self.bidderBankAcno = bidderBankAcno
self.bidderIfscCode = bidderIfscCode
self.paymentArray = paymentArray
self.chitrefArray = chitrefArray
self.ref = nil
init(snapshot: FIRDataSnapshot)
let snapshotValue = snapshot.value as! [String: AnyObject]
bidderId = snapshotValue["bidderId"] as! Int
bidderName = snapshotValue["bidderName"] as! String
bidderEmail = snapshotValue["bidderEmail"] as! String
bidderPhone = snapshotValue["bidderPhone"] as! String
modeOfPayment = snapshotValue["modeOfPayment"] as! Int
bidderBankName = snapshotValue["bidderBankName"] as! String
bidderBankAcno = snapshotValue["bidderBankAcno"] as! String
bidderIfscCode = snapshotValue["bidderIfscCode"] as! String
paymentArray = snapshotValue["paymentArray"] as? [[String:String]]
chitrefArray = snapshotValue["chitrefArray"] as? [String]
ref = snapshot.ref
func toAnyObject() -> Any
return [
"bidderId" : bidderId,
"bidderName" : bidderName,
"bidderEmail": bidderEmail,
"bidderPhone": bidderPhone,
"modeOfPayment": modeOfPayment,
"bidderBankName":bidderBankName,
"bidderBankAcno": bidderBankAcno,
"bidderIfscCode":bidderIfscCode,
"paymentArray": paymentArray!,
"chitrefArray" : chitrefArray!
]
Chit.swift
struct ChitObject
var chitId : Int
var chitName: String
var startDate: String
var endDate : String
var chitTotalAmt: Int
var chitTotalMonths: Int
var bidders: [Bidder]
var ref: FIRDatabaseReference?
init(chitId: Int,chitName: String, startDate: String, endDate: String, chitTotalAmt: Int, chitTotalMonths: Int, bidders: [Bidder])
self.chitId = chitId
self.chitName = chitName
self.startDate = startDate
self.endDate = endDate
self.chitTotalAmt = chitTotalAmt
self.chitTotalMonths = chitTotalMonths
self.bidders = bidders
self.ref = nil
init(snapshot: FIRDataSnapshot)
let snapshotValue = snapshot.value as! [String: Any]
chitId = snapshotValue["chitId"] as! Int
chitName = snapshotValue["chitName"] as! String
startDate = snapshotValue["startDate"] as! String
endDate = snapshotValue["endDate"] as! String
chitTotalAmt = snapshotValue["chitTotalAmt"] as! Int
chitTotalMonths = snapshotValue["chitTotalMonths"] as! Int
bidders = snapshotValue["bidders"] as! [Bidder]
ref = snapshot.ref
func toAnyObject() -> Dictionary<String, Any>
return [
"chitId" : chitId,
"chitName": chitName,
"startDate": startDate,
"endDate": endDate,
"chitTotalAmt":chitTotalAmt,
"chitTotalMonths": chitTotalMonths,
"bidders":bidders
]
ViewController.swift
let chitItem = ChitObject(chitId: self.chitId, chitName: chitName.text!, startDate: startDate.text!, endDate: endDate.text!, chitTotalAmt: Int(totalAmount.text!)!, chitTotalMonths:
Int(totalMonths.text!)!, bidders: biddersArray)
let chitItemRef = ChitUtils.chitRef.child((chitName.text?.lowercased())!) // Crashes here at this point.
chitItemRef.setValue(chitItem.toAnyObject(), withCompletionBlock: erorr , dataRef in
if erorr != nil
let _ = self.navigationController?.popViewController(animated: true)
)
chitItemRef.setValue(chitItem.toAnyObject())
输出: *** 由于未捕获的异常“InvalidFirebaseData”而终止应用程序,原因:“(setValue:withCompletionBlock:) 无法在bidders.0 存储_SwiftValue 类型的对象。只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。'
【问题讨论】:
我认为这条线导致了问题:toAnyObject()
中的Chit.swift
中的"bidders":bidders
,尝试"bidders":bidders.map($0. toAnyObject())
或类似的东西。
@Larme 非常感谢。效果很好
@Larme - 我在尝试将投标人的数据打印为 投标人 类类型的数组时遇到了这个问题。错误 NSArray 元素未能匹配 Swift Array 元素类型
***.com/questions/25484554/… ?你在哪里打印?你的问题到底出在哪里?
@Larme - 当我尝试 print(self.chitsArray[0].bidders) 我认为这是因为上面的代码 "bidders" :bidders.map($0.toAnyObject()) 不确定。
【参考方案1】:
您正在尝试在 firebase 中设置 Bidder
对象数组:
在你的ChitObject
:
var bidders: [Bidder]
toAnyObject()
方法:
"bidders":bidders
Firebase 将不知道这是什么,并且根据错误消息期待 NSNumber, NSString, NSDictionary, or NSArray.
。 (它是您自己的自定义对象的数组)。
尝试将您的出价方数组映射到出价方数组中。toAnyClass() 以便它们都使用 Firebase 原始类型。
【讨论】:
我在尝试将投标人的数据打印为 投标人 类类型的数组时遇到了这个问题。错误 NSArray 元素未能匹配 Swift Array 元素类型以上是关于InvalidFirebaseData Swift 3 只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。的主要内容,如果未能解决你的问题,请参考以下文章
在 Firebase 中保存多个值时出现 InvalidFirebaseData 错误
尝试将用户电子邮件和姓名保存到 Firebase 数据库时出现异常
Xcode Firebase I 更新 Firebase 数据库值时出现问题