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?

aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • 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. – Fosco Jul 22 '14 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? – aryaxt Jul 22 '14 at 23:20

3 Answers3

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()
dhallman
  • 661
  • 6
  • 7
  • @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. – dhallman Jan 14 '16 at 22:53
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;
C0D3
  • 6,440
  • 8
  • 43
  • 67
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
  • You need to import PFObject+Subclass.h in your Objective-C bridging header, props to this guy -> https://stackoverflow.com/questions/25236760/parse-pfsubclassing-not-working-with-swift/25248838#25248838 – sachadso Sep 09 '14 at 08:13
  • Yeah this still didn't work even though I included the bridge header and changed the method signature. Any updates? – C0D3 Jan 13 '16 at 19:06
-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.

  • Of course it doesn't..... Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can only call +registerSubclass on subclasses conforming to PFSubclassing.' – C0D3 Jan 13 '16 at 21:18