Anthony Dito Anthony Dito - 1 month ago 18
Objective-C Question

Parse.com subclassing in Swift 2

My question is about a bridge header that does not seem to work in Swift 2. I copied this code strait from the Parse.com iOS guide into xCode to see if it would work.

#import <Parse/PFObject+Subclass.h>

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

static func parseClassName() -> String {
return "Armor"
}
}


This doesn't work. I get an error on the
#import <Parse/PFObject+Subclass.h>
line with the error
Consecutive statements of a line must be separated by ';'
. So, my question is how I would go about subclassing in Parse.com with Swift 2. I have looked around the internet and haven't found anything. I think that there may have been a change in how Swift imports bridge headers, but I am not at all sure because I have never used a bridge header before. So, I could be doing something idiotic. Anyway, any help is greatly appreciated. Thanks.

Answer

First of all make sure you have the latest Parse SDK. If you have the latest SDK you can create a PFObject Subclass like this;

class Armor: PFObject, PFSubclassing {

static func parseClassName() -> String {
      return "Armor"
   }
}

When you create your custom Subclass, you should register your subclass to Parse SDK in AppDelegate like this;

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //Registering SubClasses
        Armor.registerSubclass()

        // Initialize Parse.
        Parse.enableLocalDatastore()
        Parse.setApplicationId("Your API Key", clientKey: "Your Client Key")

        return true
    }
Comments