CoreData获取属性类型 - 如何确定它是否是基元

Pru*_*goe 1 core-data objective-c nsentitydescription nsattributedescription

我正在尝试获取实体的所有属性,然后确定它们的类型 - 我知道我可以在这一行上做点什么:

if(![[thisAttribute attributeValueClassName] isKindOfClass:[NSString class]]) {
Run Code Online (Sandbox Code Playgroud)

但是如何检查BOOL,Float或Integer?

到目前为止,这是我的代码:

//get the attributes for this entity - we need to parse the dictionary of data we want to store and convert its contents from NSStrings to whatever type the entity requires
    NSEntityDescription* entity = [NSEntityDescription entityForName:strEntityName inManagedObjectContext:myMOC];
    NSDictionary* dictAttributes = [entity attributesByName];

    for(NSString* strThisKey in dictAttributes) {

        NSAttributeDescription* thisAttribute = [dictAttributes objectForKey:strThisKey];
        NSString* strAttributeType = [thisAttribute attributeValueClassName];

        //look for a match in the data keys (the dict we passed) with the entity keys
        for(NSString* strDataKey in dictDataToStore) {

            //found
            if ([strDataKey isEqualToString:strThisKey]) {

                if(![strAttributeType isEqualToString:@"NSString"]) {

                    //check for whatever else (@"NSDate", @"NSNumber", etc.)
                }
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)

好吧,我误解了从NSAttributeDescription返回给我的内容,我编辑了代码,基本上回答了我的问题.希望这有助于其他人.

Mun*_*ndi 5

您可以使用NSEntityDescriptionNSPropertyDescriptionAPI来确定建模实体的类型.

我会通过枚举NSAttributeDescriptionNSAttributeType常量

switch (thisAttribute.attributeType) {
  case NSInteger16AttributeType: { /* do something */; } break;
  case NSDecimalAttributeType  : { /* do something */; } break;
  case NSStringAttributeType   : { /* do something */; } break;
  case NSBooleanAttributeType  : { /* do something */; } break;
  // etc
  default: break;
}
Run Code Online (Sandbox Code Playgroud)