NSEntityDescription和NSAttributeDescription如何工作?

时间:2011-02-21 08:29:54

标签: core-data ios4 ipad

我试图理解这些东西是如何工作的:NSEntityDescription,NSAttributeDescription,attributeType。

我认为这几行代码有效,因为我得到了我对X值的期望。 有人可以告诉我应该如何修改循环的内部部分,即行:X ++; 为了获得实体中属性的名称和类型:“myentity”?

//::::::::::::::::::::::::::: EXPERIMENT    
MeDaSyAGAppDelegate *TheAppDelegate=[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *TheContext=[TheAppDelegate managedObjectContext];
NSEntityDescription *TheEntityDesc;
TheEntityDesc=[NSEntityDescription entityForName:@"myentity" inManagedObjectContext:TheContext];
int X=0;
NSDictionary *attribs=[TheEntityDesc attributesByName];
for (NSAttributeDescription *eachA in [attribs allValues]) {
    X++;
}       
[self showMessageBox:[NSString stringWithFormat:@"X = %d.",X]];
//::::::::::::::::::::::::::: EXPERIMENT

1 个答案:

答案 0 :(得分:2)

首先:格式化代码。见下文。

第二:尝试这样做:

NSLog(@"%@",eachA.name);//The name
NSLog(@"%d",[eachA attributeType])//The type, this is an integer
NSLog(@"%@",[eachA attributeValueClassName]);//Class name receiver

请参阅:NSAttributeDescription Class Docs

格式化:这看起来更好。 (属性从小写开始并使用空格)

//::::::::::::::::::::::::::: EXPERIMENT    
MeDaSyAGAppDelegate *theAppDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *theContext = [TheAppDelegate managedObjectContext];
NSEntityDescription *theEntityDesc = [NSEntityDescription entityForName:@"myentity" inManagedObjectContext:TheContext];
int X = 0;
NSDictionary *attribs = [theEntityDesc attributesByName];
for (NSAttributeDescription *eachA in [attribs allValues]) {
    X++;
}       
[self showMessageBox:[NSString stringWithFormat:@"X = %d.", X]];
//::::::::::::::::::::::::::: EXPERIMENT