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

8 iphone core-data objective-c

对于任何给定的NSManagedObject,是否有任何方法可以返回该特定NSManagedObject的一组或一组键(属性名称)?我试过在NSObject和NSManagedObject文档中查找,但一无所获.像NSDictionary'allKeys'那样起作用的东西就是我需要的东西,即.

myArrayOfKeys = [myDict allKeys]
Run Code Online (Sandbox Code Playgroud)

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

小智 14

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


Slo*_*ner 5

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

// 来自 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;
Run Code Online (Sandbox Code Playgroud)