Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以使用变量动态更改代码中类的名称?_Ios_Class_Variables_Instance - Fatal编程技术网

Ios 是否可以使用变量动态更改代码中类的名称?

Ios 是否可以使用变量动态更改代码中类的名称?,ios,class,variables,instance,Ios,Class,Variables,Instance,我有这个功能: - (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 =

我有这个功能:

- (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 @"";
}
-(NSString*)getId:(id)id_字段with column:(int)test_列with table:(NSString*)tableName//renvoyer le label
{
n错误*错误=nil;
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*实体=[NSEntityDescription entityForName:tableName
inManagedObjectContext:managedObjectContext];
[FetchRequestSetEntity:entity];
NSArray*fetchedObjects=[managedObjectContext executeFetchRequest:fetchRequest错误:&error];
for(*获取对象中的信息)
{
如果(测试列==LBL2\u CLMN)
{
NSLog(@“info.id:%@”,info.id);
if([info.id比较:id\u字段]==sensorderedName)
NSLog(@“info.id:%@”,info.label1);
返回信息标签1;
}
else if(测试列==LBL1\U CLMN)
{
if([info.id比较:id\u字段]==sensorderedName)
返回信息标签2;
}
}
返回@”;
}
如何使用变量tableName更改instanciate*info类的名称


可能吗?

不是直接的,而是因为
executeFetchRequest
返回
NSManagedObject
在重复循环中使用它,并将对象强制转换到
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
子句中缺少一对大括号。

您必须使用NSClassFromString方法,然后使用id关键字来获取对象:

NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:table inManagedObjectContext:managedObjectContext];

[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

Class theClass = NSClassFromString(table);
id info = [theClass new];

for (info in fetchedObjects)
{
   .....
}

return @"";

非常感谢,你击中了目标:一个小东西。我不知道如何灌输:ClassA*info=(ClassA*)对象;使用tableName变量…?不可能在运行时创建(评估)动态变量或类名。但是如果([tableName IseQualtString:@“table1”]”)和分别强制转换对象,则可以执行
操作。在某些情况下,您必须强制转换类型,例如
[(Table_civility*)info valueForKey:@“label1”]
,如前所述,运行时不可能进行动态键入,因为必须在编译时对类型进行求值。哇,你真是个神。