CoreData получает тип атрибута - как определить, является ли он примитивом
Я пытаюсь получить все атрибуты для сущности, а затем определить их тип - я знаю, что могу что-то сделать в этой строке:
if(![[thisAttribute attributeValueClassName] isKindOfClass:[NSString class]]) {
но как мне проверить 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.)
}
}
}
}
Хорошо, я неправильно понял, что мне возвращали из NSAttributeDescription, я отредактировал код и по существу ответил на мой вопрос. Надеюсь, это поможет кому-то еще.
1 ответ
Решение
Вы можете использовать NSEntityDescription
а также NSPropertyDescription
API для определения типа моделируемого объекта.
Я бы перечислил через NSAttributeDescription
"s 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;
}