我正在尝试获取实体的所有属性,然后确定它们的类型 - 我知道我可以在这一行做一些事情:
if(![[thisAttribute attributeValueClassName] isKindOfClass:[NSString class]]) {
但是如何检查 BOOL、浮点数或整数?
到目前为止,这是我的代码:
//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 返回给我的内容,我编辑了代码并基本上回答了我的问题。希望这可以帮助其他人。