假设我有这样的模型:
#import <Mantle/Mantle.h>
#import "MyCustomObject.h"
@interface MyModel : MTLModel <MTLJSONSerializing>
@property (nonatomic, copy, readonly) NSString *UUID;
@property (nonatomic, copy) NSString *someProp;
@property (nonatomic, copy) MyCustomObject *anotherProp;
@end
#import "MyModel.h"
@implementation MyModel
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"UUID": @"id",
@"anotherProp": NSNull.null
};
}
}
@end
如您所见,我想anotherProp
在 NSCoding 序列化期间忽略,并将“UUID”重新映射到“id”。使用 YapDatabase,我做了一个
[transaction setObject:myModelObj forKey:@"key_1" inCollection:@"my_collection"]
anotherProp
但尽管有我的自定义方法,它仍会尝试序列化JSONKeyPathsByPropertyKey
,导致此错误:
*** Caught exception encoding value for key "anotherProp" on class MyModel: -[YapDatabase encodeWithCoder:]: unrecognized selector sent to instance 0xc989630
我是否需要编写自定义序列化程序才能使用 YapDatabase JSONKeyPathsByPropertyKey
?