title: Firebase iOS Swift fatal error: NSArray element failed to match the Swift Array Element type
body: <p>I am trying to fetch data from firebase database the format of data is below.</p>
  
  <pre><code>{
    "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
          }
        ],
        "chitId": "Chit-1",
        "chitName": "ABC1",
        "chitTotalValue": 240000,
        "endDate": "11/10/2018",
        "monthlyShare": 1000,
        "startDate": "11/10/2017",
        "totalMonths": 12
      }
    }
  }
  </code></pre>
  
  <p>According to the above JSON, I have created models </p>
  
  <blockquote>
    <p><strong>Bidder.swift</strong></p>
  </blockquote>
  
  <pre><code>    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() -&gt; 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
          ]
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p><strong>ChitObject.swift</strong></p>
  </blockquote>
  
  <pre><code>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() -&gt; Dictionary&lt;String, Any&gt;
      {
          return [
              "chitId" : chitId,
              "chitName": chitName,
              "startDate": startDate,
              "endDate": endDate,
              "chitTotalAmt":chitTotalAmt,
              "chitTotalMonths": chitTotalMonths,
              "bidders":bidders.map({$0.toAnyObject()})
          ]
      }
  
  }
  </code></pre>
  
  <p>However, In Viewcontroller I am trying to fetch the <strong>biddersList</strong> from the code below.</p>
  
  <pre><code>var chitsArray = [ChitObject]()
   ChitUtils.chitRef.queryOrdered(byChild: "chitId").observe(.value, with: { snapshot in
              if snapshot.childrenCount &gt; 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()
              }
          })
  </code></pre>
  
  <blockquote>
    <p>The <strong>fatal error</strong> only occurs while trying to access <strong>bidders array</strong> at <code>print(self.chitsArray.first!.bidders)</code>. But, the array count seems to be correct all the time. </p>
  </blockquote>