我有一个通用方法,如下所示:

-(NSArray *) db_select: (NSString *) entity where: (NSString*) fieldKey equals: (NSString*) value withSortField: (NSString *) sortField withFetchLimits:(NSRange) fetchLimits{
// convert value to a number if it isn't a string
if (value != nil && ![value isKindOfClass:[NSString class]]){
    if ([value isKindOfClass:[NSNumber class]]){
        value = [((NSNumber*)value) stringValue];
    }
}

// assemble
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entity inManagedObjectContext:moContext];
if (fieldKey != nil){
    NSPredicate *predicate = [NSPredicate
                              predicateWithFormat:@"%K like %@",
                              fieldKey,value];
    [request setPredicate:predicate];
}
[request setEntity:entity];
[request setFetchLimit:fetchLimits.length];
[request setFetchOffset:fetchLimits.location];

if (sortField != nil){
    NSSortDescriptor *sortDescriptor = nil;
               if (/*TODO fieldKey refers to an NSString */YES){
                         sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self extractSortField:sortField] ascending:[self isAscending:sortField]  selector:@selector(localizedCaseInsensitiveCompare:)];
               } else {
                         sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self extractSortField:sortField] ascending:[self isAscending:sortField]];
               }
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
}

// make the request
NSError *error;
return [moContext executeFetchRequest:request error:&error];


}

如果字段(aka fieldKey)不是字符串,则我不想执行localizedCaseInsensitiveCompare。如何查询核心数据架构并确定entity.fieldKey是否为字符串?

谢谢!

最佳答案

您可以通过其实体作为实例:

NSEntityDescription *desc = [myEntity entity];
NSAttributeDescription *attDesc = [[desc propertiesByName] valueForKey:@"myProperty"];
NSAttributeType *type = [attDesc attributeType];


从那里开始,一个简单的开关将确定您要处理的内容。

关于iphone - 查询核心数据属性的类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8176666/

10-11 14:53