我有这个功能:
- (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更改instanciate * info类 的名称?
有可能吗?
答案 0 :(得分:3)
不是直接的,但是因为if - else
返回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
范围中的预期类。
n
我猜第二个m
子句中缺少一对括号。
答案 1 :(得分:1)
您必须使用NSClassFromString方法,然后使用id关键字来获取对象:
getPageTopLeft(el) {
const rect = el.getBoundingClientRect();
const docEl = document.documentElement;
return {
left: rect.left + (window.pageXOffset || docEl.scrollLeft || 0),
top: rect.top + (window.pageYOffset || docEl.scrollTop || 0)
};
}