0

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!

10
  • You are using NewPFObject then suddenly switch to MyCustomClassInParse is that a typo or are you really using two different class names? Have you used the debugger to look at dataObjects and see what is inside the NSArray? Commented Dec 19, 2015 at 19:56
  • @Paulw11, the "MyCustomClassInParse" is the actual class in the Parse cloud. NewPFObject is the Physical class on the device (The Script). The dataObjects contains PFObject's as a Subclass of NewPFObject, that's why I'm confused. Commented Dec 19, 2015 at 20:03
  • @Paulw11, I'm pretty sure that the PFQuery doesn't have anything to do with the PFObject or the NewPFObject. Commented Dec 19, 2015 at 20:05
  • The class name returned by parseClassName must 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 Commented Dec 19, 2015 at 20:09
  • @Paulw11 Wow. That is actually it. You are completely right. But how would I go about making this work universally / dynamic with any Parse Class then? Commented Dec 19, 2015 at 20:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.