Alonzo
Alonzo

Reputation: 783

Could not init a PFObject subclass

After upgrading to Xcode 6.3 6D570 (and Swift 1.2), the init of a subclassed object does not compile. Let's say I have a class called Armor, which inherits from PFObject, PFSubclassing (exactly as Parse documentation says).

When I try to create an instance, like var armor = Armor(), I get the following compile error:

Missing argument for parameter 'className' in call

Then I read in Parse docs that I should use the 'object' class method to init a subclassed object. So I tried to init like this: var armor = Armor.object().

But then I get the following compile error:

'object()' is unavailable: use object construction 'PFObject()'

I'm using Parse SDK version 1.7.1.

I also override the parseClassName method as follows:

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

I registered the subclass inside the initialise method and on app delegate before I setup Parse:

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

How should I properly init a subclassed object?

Upvotes: 1

Views: 968

Answers (2)

Bartek Chlebek
Bartek Chlebek

Reputation: 1665

=========================================================================

THIS HAS BEEN FIXED IN 1.7.3 PARSE SDK You can download the new version here: https://parse.com/docs/downloads

=========================================================================

Though docs are not clear on that, using .object() in Swift is not required anymore. as said here and in the Swift code snippet found here

Now, this is weird but to make Armor() work you need to reference PFUser class somehow in the same file. Maybe it doesn't have to be PFUser but I havent dug deeper into it.

So, this won't work

import UIKit
import Parse
import Bolts

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        Parse.setApplicationId("appID", clientKey: "clientKey")

        let myArmor = Armor()

        return true
    }
}

But this will

import UIKit
import Parse
import Bolts

private let fix = PFUser.hash() // here's the weird trick

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        Parse.setApplicationId("appID", clientKey: "clientKey")

        let myArmor = Armor()

        return true
    }
}

Hope this helps and Parse pushes a fix out soon.

Also reported as a bug

Upvotes: 4

siegy22
siegy22

Reputation: 4413

register the class as a subclass in the AppDelegate, but before Parse.initialize is called. Then you can delete the overwritten function initialize

This would make:

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

        Armor.registerSubclass()

        // Further initialization

        return true
    }

then just initialize the class by calling the constructor:

var myArmor = Armor()

Upvotes: 0

Related Questions