我使用 PFObject 的以下子类:
//HEADER FILE
#import <Parse/Parse.h>
@interface ParseMenuItem : PFObject <PFSubclassing>
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * menuItemDescription;
@property (nonatomic, strong) NSArray * menuPortions; //Some other PFObjects...
@property (nonatomic, strong) NSArray * categories; //NSStrings
//...
@end
//IMPLEMENTATION FILE
#import <Parse/PFObject+Subclass.h>
#import "ParseMenuItem.h"
@implementation ParseMenuItem
@dynamic name, menuItemDescription, menuPortions, categories;
+ (NSString*) parseClassName {
return @"MenuItem";
}
//...
@end
每次我尝试访问
menuItem.categories
或 menuItem.menuPortions
在哪里 menuItem
是 ParseMenuItem
类型的获取对象, 异常(exception) Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Key "categories" / "menuPortions" has no data. Call fetchIfNeeded before getting its value.'
被提出。如何自定义这些属性的 getter,以便在需要时自动获取数据?
我不知道动态生成的 getter 是什么样子的,所以我不知道我必须写什么来用一些自定义的 getter 替换它们。
有没有办法在我提供的那个中调用动态生成的?
最佳答案
在访问之前,您需要先检查 key 以查看它是否已定义/是否有数据。让它在 getter 中获取数据是一个好主意,但它必须是异步的,所以除非你提供一个 block ,否则它不能返回值。
您也许可以定义一个至少可以安全检查而不会抛出的 getter:
-(NSString *) menuItemDescription {
if (menuItemDescription) {
return menuItemDescription;
}
return nil;
}
编辑:您是否尝试过在 setter 中进行阻塞获取?就像是:
-(NSArray *) categories {
if (!categories) {
[self fetchIfNeeded];
}
if (!categories) {
return nil;
}
return categories;
}
关于ios - 子类化 PFObject : Proper way to override dynamic getter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23128146/