2

I've subclassed the PFObject with the following Class (dynamic properties, and +load & parseClassName Methods in .m file)

@interface DAOpponents : PFObject <PFSubclassing>
@property (nonatomic, strong) PFObject* fromUser;
@property (nonatomic, strong) PFObject* toUser;
@property (nonatomic) BOOL isVisible;
@property (nonatomic) BOOL isPersistent;
+ (NSString *)parseClassName;
@end

In a Unit Test I try to create a Sample DAOpponents-Object:

DAOpponents* follow = [DAOpponents object];
follow.fromUser = user1; // caught "NSInvalidArgumentException", "PFObject values may not have class: PFUser"
follow.toUser = user2;
[follow save];

If I do the same without a subclassed Version of the opponents there's no Exception

PFObject* follow = [[PFObject alloc] initWithClassName:@"DAOpponents"];
[follow setObject:user1 forKey:@"fromUser"]; // no exception!!!
[follow setObject:user1 forKey:@"toUser"];

Why does a subclassed PFObject can not point to a PFUser object?

Any help appreciated! Thanks a lot!

cakl
  • 390
  • 1
  • 3
  • 13
  • Have you tried declaring fromUser and toUser as `PFUser*` not `PFObject*`? – Paulw11 Oct 11 '14 at 19:40
  • @Paulw11 Yes, I even tried to override the setter / getter manually (without dynamic generation) and a cast "(PFObject*)user1;" too. – cakl Oct 11 '14 at 19:43

1 Answers1

2

I was able to get around this problem by first subclassing PFUser, then using my subclass called User in my other PFObject subclasses.

My PFUser subclass, User.h:

#import <Parse/Parse.h>

@class Person;
@class Parish;

@interface User : PFUser<PFSubclassing>
//+ (NSString *)parseClassName;

// Return the current user
+ (User *)currentUser;

@property (retain) NSNumber *acceptedAgreements;
@property (retain) NSNumber *isAdmin;
@property (retain) Person *person;

@end

and my User.m:

#import "User.h"
#import <Parse/PFObject+Subclass.h>
#import "Person.h"

@implementation User

@dynamic acceptedAgreements;
@dynamic isAdmin;
@dynamic person;

//+ (NSString *)parseClassName {
//    return @"User";
//}

// Return the current user
+ (User *)currentUser {
    return (User *)[PFUser currentUser];
}

@end
Ryan Kreager
  • 3,571
  • 1
  • 20
  • 35
  • Thanks, it works but: I have to override all static methods of PFUser? Is that a bug or why I've to do this? – cakl Oct 12 '14 at 16:20
  • PFUser is a special superclass of PFObject (so sayeth the parse.come employee here: https://www.parse.com/questions/get-pfuser-in-pfquery-using-ios-api), and so you need to treat its instantiation a little differently than if it was a subclass. I don't think you have to override all the static methods - I haven't needed to do so. – Ryan Kreager Oct 12 '14 at 20:36
  • And why do you override `currentUser`? `[PFUser currentUser]` returns an `instancetype`. Called on User this should return a User Object (and not a PFUser). Or am I wrong? – cakl Oct 12 '14 at 20:55
  • Sorry, that's a convenience method for my particular project - I think you can safely ignore it :) – Ryan Kreager Oct 12 '14 at 21:29