我有这个功能:
- (NSString*) getId:(id)id_field withColumn:(int)test_column withTable:(NSString *) tableName //renvoyer le label
{
NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:tableName
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (<tableName of class> *info in fetchedObjects)
{
if (test_column == LBL2_CLMN)
{
NSLog(@"info.id :%@", info.id);
if ([info.id compare:id_field] == NSOrderedSame)
NSLog(@"info.id :%@", info.label1);
return info.label1;
}
else if (test_column == LBL1_CLMN)
{
if ([info.id compare:id_field] == NSOrderedSame)
return info.label2;
}
}
return @"";
}
如何更改班级名称使用变量 tableName 实例化 *info ?
是否可以 ?
不是直接的,而是自从executeFetchRequest
回报NSManagedObject
在重复循环中使用它并将对象转换为预期的类if - else
scopes.
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *object in fetchedObjects)
{
if (test_column == LBL2_CLMN)
{
ClassA *info = (ClassA *)object;
NSLog(@"info.id :%@", info.id);
if ([info.id compare:id_field] == NSOrderedSame) {
NSLog(@"info.id :%@", info.label1);
return info.label1;
}
}
else if (test_column == LBL1_CLMN)
{
ClassB *info = (ClassB *)object;
if ([info.id compare:id_field] == NSOrderedSame)
return info.label2;
}
}
return @"";
我猜第二个缺少一对大括号if
clause.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)