Kevin Jensen Petersen
Kevin Jensen Petersen

Reputation: 513

PFObject Subclassing: NSArray element failed to match the Swift Array Element type

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!

Upvotes: 0

Views: 261

Answers (1)

Paulw11
Paulw11

Reputation: 114773

The value you return from parseClassName must match the class name that is defined in Parse.com, so in your case, parseClassName would need to return MyCustomClassInParse

This enables the Parse framework to match the PFSubclassing class to the Parse.com class and return the appropriate object. If there is no match then you will just get plain-old PFObject instances, which is why you get a runtime error when you try to downcast.

Upvotes: 1

Related Questions