如何使用字符串为Objective-C中的变量声明指定类

时间:2021-11-11 19:56:35

I am trying to use a string that is passed into a method to cast the objects in an array to that type and iterate over them.

我试图使用传递给方法的字符串将数组中的对象强制转换为该类型并迭代它们。

My code looks like this:

我的代码如下所示:

- (NSArray *)serializableEntities:(NSArray *)entities forEntityName:(NSString *)entityName
{
    NSMutableArray *seriazliedEntities = [[NSMutableArray alloc] init];

    for (int i=0; i < [entities count]; i++) {

        entityName *entityObj = (entityName *) [entities objectAtIndex:i];

        ...
    }
}

How can I do this? Is this possible?

我怎样才能做到这一点?这可能吗?

I have tried doing it like so but believe I am missing the correct method/syntax:

我试过这样做,但相信我错过了正确的方法/语法:

Class objectClass = NSClassFromString(entityName);
objectClass *myObject = (objectClass *) [entities objectAtIndex:i];

2 个解决方案

#1


3  

(From my and Ken's comment:) That is not possible. You can only cast to a type (which is something known at compile-time). You can use the generic Objective-C type id:

(从我和肯的评论:)这是不可能的。您只能转换为类型(在编译时已知)。您可以使用通用的Objective-C类型id:

id entityObj = [entities objectAtIndex:i];

If all objects have a common superclass, use that:

如果所有对象都有一个共同的超类,请使用:

SuperClass *entityObj = [entities objectAtIndex:i];

or perhaps all objects conform to a common protocol:

或者所有对象都符合通用协议:

id<CommonProtocol> entityObj = [entities objectAtIndex:i];

Of course you can check the actual class of the object at runtime, for example

例如,您可以在运行时检查对象的实际类

id entityObj = [entities objectAtIndex:i];
if ([entityObj isKindOfClass:NSClassFromString(entityName)]) {

}

#2


0  

The key was to using the superclass of the NSManagedObject subclasses rather than trying to determine the actual subclass. This allowed me to remove the code trying to figure out what class it was. Here is the working solution:

关键是使用NSManagedObject子类的超类而不是尝试确定实际的子类。这允许我删除代码,试图找出它是什么类。这是工作解决方案:

- (NSArray *)serializableEntities:(NSArray *)entities
{
    NSMutableArray *seriazliedEntities = [[NSMutableArray alloc] init];

    for (int i=0; i < [entities count]; i++) {

        NSManagedObject *entityObj = [entities objectAtIndex:i];

        NSMutableDictionary *serializedEntity = [[NSMutableDictionary alloc] init];

        for (NSString *key in [[entityObj entity] attributesByName]) {
            if ([entityObj valueForKey:key]) [serializedEntity setValue:[self nullCheck:[entityObj valueForKey:key]] forKey:key];
        }

        [seriazliedEntities addObject:serializedEntity];
    }

    return seriazliedEntities;
}

#1


3  

(From my and Ken's comment:) That is not possible. You can only cast to a type (which is something known at compile-time). You can use the generic Objective-C type id:

(从我和肯的评论:)这是不可能的。您只能转换为类型(在编译时已知)。您可以使用通用的Objective-C类型id:

id entityObj = [entities objectAtIndex:i];

If all objects have a common superclass, use that:

如果所有对象都有一个共同的超类,请使用:

SuperClass *entityObj = [entities objectAtIndex:i];

or perhaps all objects conform to a common protocol:

或者所有对象都符合通用协议:

id<CommonProtocol> entityObj = [entities objectAtIndex:i];

Of course you can check the actual class of the object at runtime, for example

例如,您可以在运行时检查对象的实际类

id entityObj = [entities objectAtIndex:i];
if ([entityObj isKindOfClass:NSClassFromString(entityName)]) {

}

#2


0  

The key was to using the superclass of the NSManagedObject subclasses rather than trying to determine the actual subclass. This allowed me to remove the code trying to figure out what class it was. Here is the working solution:

关键是使用NSManagedObject子类的超类而不是尝试确定实际的子类。这允许我删除代码,试图找出它是什么类。这是工作解决方案:

- (NSArray *)serializableEntities:(NSArray *)entities
{
    NSMutableArray *seriazliedEntities = [[NSMutableArray alloc] init];

    for (int i=0; i < [entities count]; i++) {

        NSManagedObject *entityObj = [entities objectAtIndex:i];

        NSMutableDictionary *serializedEntity = [[NSMutableDictionary alloc] init];

        for (NSString *key in [[entityObj entity] attributesByName]) {
            if ([entityObj valueForKey:key]) [serializedEntity setValue:[self nullCheck:[entityObj valueForKey:key]] forKey:key];
        }

        [seriazliedEntities addObject:serializedEntity];
    }

    return seriazliedEntities;
}