PFObject 子类,无法识别的选择器发送到自定义方法的实例

PFObject subclass, unrecognized selector sent to instance for custom method

我有一个 PFObject Subclass SomeClass,我向其中添加了一个方法 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

但在调用

之后

[object iconImageName] 我得到

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

我可以确认对象确实是 SomeClass

当我使用 class 方法时也会发生这种情况 +

根据documentation,您忽略了一些子classing 规则:

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:.

尝试为您的 class 满足此规则。

示例:

// 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