3

I'm trying to get a standard subclassing to work with swift.

Bridging-Header.h

#import <Parse/Parse.h>
#import <Parse/PFObject+Subclass.h>
#import <Parse/PFGeoPoint.h>

Subclass

class Event: PFObject, PFSubclassing {

    class func parseClassName() -> String! {
        return "Event"
    }

    override class func load() {
        registerSubclass()
    }
}

getting a compile error saying that Event does not conform to PFSubclassing.

Any suggestions?

2
  • This should work with the latest SDK version, 1.2.20. In the version you have, the return type for objectWithoutDataWithClassName in PFObject.h needs to be changed to instancetype from id. Commented Jul 22, 2014 at 23:13
  • @Fosco I'm using cocoapods and the latest version available is 1.2.19. Any idea when the latest version would be submitted there? Commented Jul 22, 2014 at 23:20

3 Answers 3

2

Check out my Parse Subclass generator GSParseSchema. It can generate the Swift and Objective-C classes for you.

In Swift, you'll want to override the initialize function.

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()
    }
}

I've found this auto-registration to not always work, so I also explicitly register the subclasses in my AppDelegate. Be sure you register you before you initialize Parse with setApplicationId:clientKey:

Event.registerSubclass()
Sign up to request clarification or add additional context in comments.

1 Comment

@c0d3Junk13 Please note that the initialize method is not called until the class receives its first message, meaning that you need to call any instance or class method on your subclass before it will be registered with Parse SDK.
1

Check out this thread. Here is an explanation of the problem:

I already opened a bug to Parse about this point. Basically the issue is with the Parse header files where the protocol definition inside PFSubclassing.h defines this method in this way: + (instancetype)objectWithoutDataWithObjectId:(NSString )objectId; but then the same method is implemented by PFObject+Subclass.h in this way (notice the difference: instancetype --> id) + (id)objectWithoutDataWithObjectId:(NSString )objectId; This is enough for Swift to complain. The only solution I found is to directly change the header in the framework definition by replacing "id" with "instancetype". If you do this, the code will compile.

So, here's your solution, and it's worked for me; I had the same problem:

  • Open PFSubclassing.h (note that this is the PFSubclassing.h under your Parse directory)
  • Find the objectWithoutDataWithObjectId: method
  • Replace the method signature with: (id)objectWithoutDataWithObjectId:(NSString *)objectId;

2 Comments

You need to import PFObject+Subclass.h in your Objective-C bridging header, props to this guy -> stackoverflow.com/questions/25236760/…
Yeah this still didn't work even though I included the bridge header and changed the method signature. Any updates?
-1

For the latest version of Parse 1.9.1 and Swift 2.1, then the following applies:

  • No need to show that the class conforms to the protocol PFSubclassing, the following is enough

class Event : PFObject { // code }

  • If you use pods then you dont need to include Parse, PFSubclassing everywhere

A example can look like the following:

class Event: PFObject {

// @NSManaged gives you autosuggest and type check
@NSManaged var var1: String? 
@NSManaged var var2: String?

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()
    }
  }
}

I will try your class generator, feels like I can save a lot of time there. Have been researching the best way to map Swift objects to Parse. I built my structure from a UML Class Diagram and added the relations set in the Parse DB to the diagram. Not best practice though it gives me what I need.

Going to use your generator, and design it as 4 tier with 4th tier being Parse DB and tier 3-1 being MVC, with M generated with your code and the needed logic from the UML Class diagram.

1 Comment

Of course it doesn't..... Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can only call +registerSubclass on subclasses conforming to PFSubclassing.'

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.