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];
NSCoder, it may serve your needs.entityNameis a run-time value and variable types are pretty much a purely compile-time thing which don't survive into the binary, what you're trying to do is meaningless. Just useidas the type.