0

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];
5
  • Why do you need to cast that objects? What do you do inside the loop? Commented Nov 9, 2013 at 9:28
  • 2
    You can only cast to a type, not to a class object. What are you trying to achieve? Commented Nov 9, 2013 at 9:29
  • Have a look at NSCoder, it may serve your needs. Commented Nov 9, 2013 at 9:54
  • 2
    Agree with Martin. Also, since your entityName is 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 use id as the type. Commented Nov 9, 2013 at 10:06
  • Thanks @SviatoslavYakymiv. I removed the casting. It was unneeded. Commented Nov 9, 2013 at 19:28

2 Answers 2

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:

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)]) {

}
Sign up to request clarification or add additional context in comments.

3 Comments

You're right. I've removed the casting as it was not needed. I was trying to iterate over NSManagedObject subclasses (different objects used in the app) and generate JSONserialized versions of key/value pairs that were not null. I've come up with the solution, posted below.
@MAzam: I do not see a solution posted by you. - I have tried to answer your question as good as possible with the provided information. If all objects are NSManagedObject subclasses, you can use NSManagedObject *entityObj = ..., as suggested above. - If your problem is solved, you should either accept an answer or post+accept your own answer.
Got distracted with family stuff. Solution is posted now.
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:

- (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 Comment

To remove the question from the "unanswered" list, you should "accept" one of the answers by clicking on the check mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.