[英]CoreData getting attribute type - how to determine if it is a primitive
我正在嘗試獲取實體的所有屬性,然后確定它們的類型-我知道我可以在此行上做一些事情:
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返回的內容,我編輯了代碼並從本質上回答了我的問題。 希望這可以幫助其他人。
您可以使用NSEntityDescription
和NSPropertyDescription
API來確定建模實體的類型。
我將通過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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.