我试图从firebase数据库中获取数据,数据格式如下。
{
"chits": {
"Chit-1": {
"Bidders": [
{
"bankAccNumber": "5555566966",
"bankIFSC": "GFHJJHHJ",
"bankName": "55666",
"bidderEmail": "[email protected]",
"bidderId": "Bidder-1",
"bidderName": "Venkat Desi",
"bidderPhone": "9999999999",
"checked": false,
"paymentType": 66555
},
{
"bankAccNumber": "5555566966",
"bankIFSC": "GFHJJHHJ",
"bankName": "55666",
"bidderEmail": "[email protected]",
"bidderId": "Bidder-2",
"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
}
}
}
根据上面的JSON,我已经创建了模型
投标人.swift
class Bidder {
var bidderId: Int
var bidderName: String
var bidderEmail: String
var bidderPhone : String
var modeOfPayment: Int
var bidderBankName: String
var bidderBankAcno: String
var bidderIfscCode: String
var paymentArray : [[String:String]]?
var chitrefArray : [String]?
var 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 as Any,
"chitrefArray" : chitrefArray as Any
]
}
}
奇多宾语.swift
class 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: AnyObject]
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.map({$0.toAnyObject()})
]
}
}
但是,在Viewcontroller中,我试图从下面的代码中获取biddersList。
var chitsArray = [ChitObject]()
ChitUtils.chitRef.queryOrdered(byChild: "chitId").observe(.value, with: { snapshot in
if snapshot.childrenCount > 0
{
self.chitsArray.removeAll()
for item in snapshot.children
{
let chitItem = ChitObject(snapshot: item as! FIRDataSnapshot)
self.chitsArray.append(chitItem)
}
self.chitsArray = self.chitsArray.reversed()
print(self.chitsArray.first!.bidders)
ChitUtils.lastChitId = (self.chitsArray.first?.chitId)! + 1
self.chitsTableView.reloadData()
}
})
只有在尝试访问
print(self.chitsArray.first!.bidders)
处的投标人数组时才会出现致命错误。但是,数组计数似乎一直都是正确的。 最佳答案
问题在您的ChitObject
类中,具体如下:
bidders = snapshotValue["bidders"] as! [Bidder]
您需要创建一个投标人数组,并将其分配到Bidders变量中。循环遍历从snapshotValue[“bidders”]获得的数组,为每个值创建bidders对象,并将其分配到Swift数组中。
我没有和Firebase打过交道,但像这样的事情可能会奏效:
init(snapshot: FIRDataSnapshot) {
let snapshotValue = snapshot.value as! [String: AnyObject]
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
if let biddersArray = snapshotValue["bidders"] as? [[String:Any]] {
bidders = biddersArray.flatMap{Bidder(dict:$0)}
}
ref = snapshot.ref
}
编辑:
class Bidder {
var bidderId: Int
var bidderName: String
var bidderEmail: String
var bidderPhone : String
var modeOfPayment: Int
var bidderBankName: String
var bidderBankAcno: String
var bidderIfscCode: String
var paymentArray : [[String:String]]?
var chitrefArray : [String]?
var ref: FIRDatabaseReference?
convenience init?(dict:[String:Any]) {
guard let bidderId = dict["bidderId"] as? Int, let bidderName = dict["bidderName"] as? String, let bidderEmail = dict["bidderEmail"] as? String, let bidderPhone = dict["bidderPhone"] as? String, let modeOfPayment = dict["paymentType"] as? Int, let bidderBankName = dict["bankName"] as? String, let bidderBankAcno = dict["bankAccNumber"] as? String, let bidderIfscCode = dict["bankIFSC"] as? String else {
return nil
}
self.init(bidderId: bidderId, bidderName: bidderName, bidderEmail: bidderEmail, bidderPhone: bidderPhone, modeOfPayment: modeOfPayment, bidderBankName: bidderBankName, bidderBankAcno: bidderBankAcno, bidderIfscCode: bidderIfscCode, paymentArray: [[]], chitrefArray: [])
}
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 as Any,
"chitrefArray" : chitrefArray as Any
]
}
}