CoreData获取属性类型 - 如何确定它是否是一个原始 [英] CoreData getting attribute type - how to determine if it is a primitive

查看:134
本文介绍了CoreData获取属性类型 - 如何确定它是否是一个原始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得实体的所有属性,然后确定它们的类型 - 我知道我可以在这行上做一些事情:

I'm trying to get all the attributes for an entity and then determine their type - I know I can do something on this line:

if(![[thisAttribute attributeValueClassName] isKindOfClass:[NSString class]]) {

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

but how do I check for a BOOL, Float or 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.)
                }
            }
        }

    }

好吧,我误解了从NSAttributeDescription返回给我的是什么,我编辑了代码,基本上回答了我的问题。希望这有助于别人出去。

OK, I misunderstood what was being returned to me from NSAttributeDescription, I edited the code and essentially answered my question. Hope this helps someone else out.

推荐答案

您可以使用 NSEntityDescription NSPropertyDescription 用于确定建模实体类型的API。

You can use the NSEntityDescription and NSPropertyDescription APIs to determine the kind of a modelled entity.

我会通过枚举NSAttributeDescription NSAttributeType 常数

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;
}

这篇关于CoreData获取属性类型 - 如何确定它是否是一个原始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆