Array of NSManagedObject attributes

2019-03-19 09:04发布

I'd like to get an array of the attributes for my NSManagedObject so I can use KVO to export them. I can create an array manually and then iterate through it, however, I'd like to get this list automatically, then iterate.

2条回答
Explosion°爆炸
2楼-- · 2019-03-19 09:36

An NSManagedObject has an entity associated with it. Use NSEntityDescription's -attributesByName and -relationshipsByName. You'll get a dictionary back from each of those methods. Just ask the dicts for their -allKeys.

查看更多
▲ chillily
3楼-- · 2019-03-19 09:48

Thanks Joshua. Here's code that I used in case any one would like to see a hard example:

NSString *entityName = NSStringFromClass([myEntity class]);
NSEntityDescription *entityDescription = [self entityDescriptionWithEntityName:entityName];
NSDictionary *attributes = [entityDescription attributesByName];
NSArray *attributeNames = attributes.allKeys;
查看更多
登录 后发表回答