I'm trying to create a custom class for PFObject (Subclassing), which seems to work fine, but when I try to convert/use the custom class object, as a regular PFObject, It messes up. Here's what I'm trying to do.
First I have created the Custom Class named NewPFObject for testing reasons.
NOTICE: I AM calling NewPFObject.registerSubclass() in the AppDelegate before setting the Application Id.
class NewPFObject: PFObject, PFSubclassing {
static func parseClassName() -> String {
return "NewPFObject"
}
override init(className:String) {
super.init(className: className)
}
}
Then I have this method that's using it to make Async calls more easy and fluid:
func GetAsyncObjects(query:STARCQuery, doNext: (dataObjects:[NewPFObject]) -> ()) {
query.findObjectsInBackgroundWithBlock { (newObjects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
doNext(dataObjects: newObjects as! [NewPFObject])
}
}
}
And finally, I have the use-case, where the error happens.
let query:PFQuery = PFQuery.init(className: "MyCustomClassInParse")
GetAsyncObjects(query) { (dataObjects) -> () in
if(dataObjects.count > 0) {
for customObject in dataObjects {
//Do something with customObject data
}
}
}
The error at the use-case is the same as the title:
fatal errror: NSArray element failed to match the Swift Array Element type
And It happens on the final block of code, on the line where I use the dataObjects Array in the for loop.
When trying to cast it multiple times makes XCode say that It's redundant to do so, and It doesn't make a difference when actually running the code. Same error.
I've literally tried everything, and every post about PFSubclassing and this error on Stackoverflow, can't seem to find the solution, so I hope someone Is willing to help me out!
Thanks!
NewPFObjectthen suddenly switch toMyCustomClassInParseis that a typo or are you really using two different class names? Have you used the debugger to look atdataObjectsand see what is inside the NSArray?parseClassNamemust be the name of the class in Parse. This is how the framework maps the returned data to your custom subclass. Since it doesn't match you are just going to get an array of PFObject