Getting the correct type when subclassing PFUser

I am trying to subclass PFUser in Parse on iOS. I can subclass using the following code:

// MyUser.h #import <Parse/PFUser.h> #import <Parse/PFObject+Subclass.h> #import <Parse/PFFile.h> @interface MyUser : PFUser<PFSubclassing> @property PFFile *avatar; ...etc... @end // MyUser.m #import "MyUser.h" @implementation MyUser @dynamic avatar; ...etc... @end 

When I call [MyUser currentUser] , the returned object is of type PFUser but not of type MyUser . Passing it to MyUser* does not help, it just throws the error "unrecognized selector sent to instance 0xce9c0c0" when I try to use any custom properties, for example. user.avatar , which is defined in MyUser.

How can I get the cached currentUser object of my custom type?

+7
ios objective-c subclass
source share
3 answers

The key was streamlining the registration of subclasses and setting the application identifier.

 [Parse setApplicationId:@"FOO" clientKey:@"BAR"]; [MyUser registerSubclass]; 

Causes a problem. However, if you set the application identifier after registering the subclasses, it works as expected.

 [MyUser registerSubclass]; [Parse setApplicationId:@"FOO" clientKey:@"BAR"]; 
+20
source share

When you create a new class in Parse, you control the name of that class. For example, let's say you create a new class called "Armor".

When you create a subclass in Objective-C, you assign the created Objective-C class to the Parse class. The following is the implementation code for this Armor subclass. As you can see, the name of the Objective-C class is "Armor", and the name of the Parse class is also "Armor".

 #import "Armor.h" #import <Parse/PFObject+Subclass.h> @implementation Armor @dynamic displayName; @dynamic rupees; @dynamic fireproof; + (NSString *)parseClassName { return @"Armor"; } @end 

In this case, the Objective-C class name and Parse class name are the same. However, this should not be. If I changed the name of my Objective-C class to "BrandonsAwesomeClass", it could still be assigned to the class "Armor". If I were to create a new instance of BrandonsAwesomeClass, it would still return the Parse class type β€œArmor”

 #import "BrandonsAwesomeClass.h" #import <Parse/PFObject+Subclass.h> @implementation BrandonsAwesomeClass @dynamic displayName; @dynamic rupees; @dynamic fireproof; + (NSString *)parseClassName { return @"Armor"; } @end 

In your case, the Parse User class is always called "_User". You cannot change this name, and it does not matter what the name of your Objective-C subclass is, it will always return PFUser as a class.

This does not have a big impact on your code or your project, because you can add additional properties to the Parse "_User" class through the website and add @dynamic properties to your Objective-C subclass.

As long as you have correctly configured your subclass [MyUser currentUser] .avatar is the same as accessing the object [[PFUser currentUser] objectForKey: @ "avatar"]

I contacted a Parse blog post explaining subclasses for reference.

http://blog.parse.com/2013/03/22/stay-classy-objective-c-introducing-native-subclasses-for-parse-objects/

+1
source share

Try the [PFUser object] .

[PFUser currentUser] will return PFUser, but [PFUser object] should return your subclass if it is registered correctly.

0
source share

All Articles