executeFetchRequest throw fatal error: NSArray element failed to match the Swift Array Element type
I was able to reproduce your error message in the console. You get this message because you didn't set correctly your entity class name in the Core Data Model Editor (see image below).
Once done, you will be able to use your original code with Contact
subClass. You can learn more about Core Data and namespaces here.
FetchRequest - NSArray element failed to match the Swift Array Element type - Swift 2.0
Solution
var voitures: [NSManagedObject]? = [NSManagedObject]()
if (voitures!.count > 0) {
let voiture: NSManagedObject = voitures![indexPath.row]
print((voiture.valueForKey("nom") as? String))
}
Thread 1: Fatal error: NSArray element failed to match the Swift Array Element type
Okay, I've found the reason of this error.
My messages
array was being appended in another block of code with SBDUserMessages
, and I wasn't cast them as exact type of message (as! SBDUserMessage
).
The debugging really helped a lot. At some point I decided to call before the self.messages.append(newMessage)
(where the crash actually occured), the messaged.removeAll(), and after that I've found out that everything works fine.
Firebase iOS Swift fatal error: NSArray element failed to match the Swift Array Element type
The issue is in your ChitObject
class, specifially here:
bidders = snapshotValue["bidders"] as! [Bidder]
You would need to create an array of Bidders and assign that into bidders variable. Loop through the array you get from snapshotValue["bidders"] and create Bidders object for every value and assign that into a Swift array.
I have not workded with Firebase but something like this may work:
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
}
EDITED:
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
]
}
}
poet.books throw fatal error: NSArray element failed to match
My suggestion is to add an initializer to your Works
class:
class Works {
var nameWork = ""
var workPoet = ""
init(nameWork: String, workPoet: String) {
self.nameWork = nameWork
self.workPoet = workPoet
}
}
Then you can use it like this:
let worksArray = booksDict[poet.poetName as String] as! NSArray
for work in worksArray {
let dictionaryFromArray = work as! NSDictionary
let workObject = Works(nameWork: dictionaryFromArray["nameWork"] as! String, workPoet: dictionaryFromArray["work"] as! String)
poet.books.append(workObject)
}
poet.books throw fatal error: NSArray element failed to match
My suggestion is to add an initializer to your Works
class:
class Works {
var nameWork = ""
var workPoet = ""
init(nameWork: String, workPoet: String) {
self.nameWork = nameWork
self.workPoet = workPoet
}
}
Then you can use it like this:
let worksArray = booksDict[poet.poetName as String] as! NSArray
for work in worksArray {
let dictionaryFromArray = work as! NSDictionary
let workObject = Works(nameWork: dictionaryFromArray["nameWork"] as! String, workPoet: dictionaryFromArray["work"] as! String)
poet.books.append(workObject)
}
Why is executeFetchRequest not returning subclass objects when run under XCTest using Swift?
I had a similar question. Making my entity class public solves the issue and I get the right class type.(CoreData class miss match in unit test)
Related Topics
How to Get Nsdate with Millisecond Accuracy
How to Tell If an iOS Device Has a Gps
How to Reload Tableview of Another Uiviewcontroller in Current Viewcontroller
Cannot Assign Class Instance to Its Protocol Type
iOS Facebook Login "Given Url Is Not Allowed by the Application Configuration"
iOS Lazy-Loading of Table Images
Geofire Query on User Location
Property 'Self.*' Not Initialized at Super.Init Call
Two Buttons Inside Hstack Taking Action of Each Other
Wkwebview Setting Cookie Not Possible (iOS 11+)
Swift 3- How to Get Button in Uicollectionviewcell Work
Hide a Phone Call Completely in iOS (Jailbreak Device)
How to Hide a Bar Button Item for Certain Users
How to Make a Conical Gradient in iOS Using Core Graphics/Quartz 2D
Nscache Emptied When App Enters Background
Having Uiview Drawrect Occur in a Background Thread
Enabling Facebook Breaking Changes Feb 2013: Login Does Not Work
How to Set the Accessibility Label for a Particular Segment of a Uisegmentedcontrol