获取NSManagedObject的属性作为NSDictionary [英] Get properties of NSManagedObject as NSDictionary

查看:133
本文介绍了获取NSManagedObject的属性作为NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个从一个上下文中获取的NSManagedObject,那么如何从它的动态属性创建一个NSDictionary,而不仅仅是将每个字段显式复制到字典中呢?

If I have an NSManagedObject I've fetched from a context, how can I create an NSDictionary out of its dynamic properties without just copying each field into the dictionary explicitly?

如果我的托管对象看起来像这样,例如:

If my managed object looks like this, for example:

@implementation Track

@dynamic artist;
@dynamic group;
@dynamic purchase_url;
@dynamic title;
@dynamic file_name;
@dynamic year;

@end

从数据库中提取后,需要一个NSDictionary,它具有与每个@dynamic属性相同的属性。

After fetching from the database, in this case, I need an NSDictionary with the same properties set to each of those @dynamic properties.

推荐答案

有一个更快的方法来转换NSManagedObject到NSDictionary(来自 Matthias Bauch 响应http://stackoverflow.com/a/5664745/2294824 ):

There is a faster way to convert an NSManagedObject to an NSDictionary (from Matthias Bauch response at http://stackoverflow.com/a/5664745/2294824) :

NSArray *keys = [[[myObject entity] attributesByName] allKeys];
NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];

这篇关于获取NSManagedObject的属性作为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆