有没有办法检索给定NSManagedObject的一组或一组键?

时间:2009-09-02 08:18:58

标签: iphone objective-c core-data

对于任何给定的NSManagedObject,是否有任何方法可以返回该特定NSManagedObject的一组或一组键(属性名称)?我试过环顾NSObject& NSManagedObject文档但什么也没找到。像NSDictionary'allKeys'那样起作用的东西就是我需要的东西,即。

myArrayOfKeys = [myDict allKeys]

我认为必须有一种更简单的处理大量属性的方法,例如。迭代一组键。

2 个答案:

答案 0 :(得分:14)

-[NSManagedObject entity]返回NSEntityDescription。然后,您可以找到它的属性,特别是如果您只需要可以获取的属性名称-[NSEntityDescription attributesByName],这是一个字典,其中每个键都是属性名称,每个值都是NSAttributeDescription

答案 1 :(得分:4)

我根据詹姆斯的建议编写了以下内容,并假设其他人对其代码进行故障排除并使用他的答案可能会有用;谢谢詹姆斯!

//来自Apple的Master Detail模板项目 - (void)insertNewObject:(id)sender {

//! Apple standard template code
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

//! Slowburner addition to view the managedObject's keys
NSEntityDescription *attDesc = [newManagedObject entity];
NSDictionary *attributesByName = [attDesc attributesByName];
NSLog(@"Names:%@",[attributesByName allKeys]);

//! shortcut to avoid whatever problem you're troubleshooting
return;