Search code examples
iosobjective-cparse-platformparse-framework

PFObject subclass, unrecognized selector sent to instance for custom method


I have a PFObject Subclass SomeClass, to which i added a method iconImageName

.h

@interface SomeClass : PFObject

@property (nonatomic, strong) NSDictionary * availableAttributes;

@property (nonatomic, strong) NSString * type;

- (NSString *)iconImageName;

@end

.m

@implementation SomeClass

@dynamic availableAttributes;
@dynamic type;

+ (NSString *)parseClassName {
    return NSStringFromClass([self class]);
}

- (NSString *)iconImageName {
    return [NSString stringWithFormat:@"icon-type-%@", self.type];
}

@end

but after calling

[object iconImageName] i get

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFObject iconImageName]: unrecognized selector sent to instance 0x174133b00'

i can confirm that the object is indeed SomeClass

this also happens when i use a class method +


Solution

  • According to documentation you ignored some subclassing rules:

    To create a PFObject subclass:
    1. Declare a subclass which conforms to the PFSubclassing protocol.
    2. Implement the class method parseClassName. This is the string you would pass to initWithClassName: and makes all future class name references unnecessary.
    3. Import PFObject+Subclass in your .m file. This implements all methods in PFSubclassing beyond parseClassName.
    4. Call [YourClass registerSubclass] before Parse setApplicationId:clientKey:.

    Try to satisfy this rules for your class.

    Example:

    // Armor.h
    @interface Armor : PFObject<PFSubclassing>
    + (NSString *)parseClassName;
    @end
    
    // Armor.m
    // Import this header to let Armor know that PFObject privately provides most
    // of the methods for PFSubclassing.
    #import <Parse/PFObject+Subclass.h>
    
    @implementation Armor
    + (void)load {
      [self registerSubclass];
    }
    
    + (NSString *)parseClassName {
      return @"Armor";
    }
    @end