marsrover
marsrover

Reputation: 715

Parse SDK and Swift 1.2: Can´t subclass PFUser

Before updating to Swift 1.2 subclassing PFUser worked just fine, now I can´t make it work.

My custom PFUser-class:

public class CustomUser: PFUser, PFSubclassing {

    @NSManaged var fullName : String!
    @NSManaged var phone : String!

    public override class func initialize(){
        self.registerSubclass()
    }

}

When I use this class in my code the method calls still goes to the PFUser class:

reason: '-[PFUser fullName]: unrecognized selector sent to instance 0x17018fbe0'

This behavior started with Swift 1.2. I´ve updated the Parse SDK to the lastest version as well.

Upvotes: 2

Views: 861

Answers (2)

oyalhi
oyalhi

Reputation: 3994

Another solution seems to be a singleton, provided in the Parse manual as follows, which works without any problems. This code works for all subclasses not only PFUser. If done this way, there is no need to register the subclass in didFinishLaunchingWithOptions.

class User: PFUser, PFSubclassing {
    // MARK: PFUser Subclassing

    override class func initialize() {
        struct Static {
            static var onceToken : dispatch_once_t = 0;
        }

        dispatch_once(&Static.onceToken) {
            self.registerSubclass()
        }
    }
    // non essential code removed
}

Upvotes: 0

rickerbh
rickerbh

Reputation: 9913

I've just been through this. The change in behaviour is a huge pain. You need to register your subclasses before you set your Parse app ID (typically in your application delegate).

So, in your app delegate...

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
  CustomUser.registerSubclass()
  Parse.setApplicationId("XXX", clientKey: "YYY")
  ......
  return true
}

Upvotes: 5

Related Questions