支持 NSNull 的核心数据
是否有可能让 Core Data 允许分配 NSNull ?我正在使用 JSONKit,它默认分配 NSNull
。我希望能够像这样进行反序列化:
- (void)deserialize:(NSDictionary *)dictionary
{
self.name = [dictionary objectForKey:@"name"];
}
而不是这样:
- (void)deserialize:(NSDictionary *)dictionary
{
NSNull *null = [NSNull null];
NSString *value = [dictionary objectForKey:@"name"];
self.name = (value != null) ? value : nil;
}
Is it at all possible to get Core Data to allow assignment of NSNull
? I'm using the JSONKit and it defaults to assigning NSNull
. I'd prefer to be able to do my deserialization like this:
- (void)deserialize:(NSDictionary *)dictionary
{
self.name = [dictionary objectForKey:@"name"];
}
Instead of like this:
- (void)deserialize:(NSDictionary *)dictionary
{
NSNull *null = [NSNull null];
NSString *value = [dictionary objectForKey:@"name"];
self.name = (value != null) ? value : nil;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种想法是为 NSDictionary 创建一个类别。该类别可以包含此行为。
One thought would be to create a category for NSDictionary. The category could then contain this behavior.
我认为使用 CoreData 不可能做到这一点。
但是,如果代码简洁,如果您正在寻找什么,您可以只使用宏:
不是我所说的优化,但带来了简洁且可读的代码:
I don't think this is possible with
CoreData
to do this.But if code concision if what you are looking for, you could just use macros :
Not what I would say optimized, but brings concise and readable code :
如果您必须处理多种集合类型(不仅仅是字典),您可以在 NSNull 上创建一个类别:
实现:
但我必须说它增加了不必要的冗长。我喜欢使用 Vincent G 的预处理器宏来保持代码可读的想法。
If you have to deal with multiple collection types (not just dictionaries), you could create a category on NSNull:
Implementation:
But I must say it adds unnecessary verbosity. I like the idea of using the Vincent G's preprocessor macros to keep the code readable.