質問

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]]) {

but how do I check for a BOOL, Float or Integer?

Here's my code so far:

//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.)
                }
            }
        }

    }

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.

役に立ちましたか?

解決

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

I would enumerate through NSAttributeDescription's NSAttributeType constants

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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top