Assistance with converting Java to Objective-C

Black20 :

I am trying to convert some code to Objective-C from Java and I'm getting stumped on converting the Java Generic portions. For example, the class declaration in Java I believe is saying that the current class extends any class that is the subclass of the Component class, in Objective-c does this mean you just extend the Component class?

Any assistance that someone could provide would be greatly appreciated. Moving forward with this will help me convert other pieces of the code that are similar. Thanks

Java:

public class ComponentMapper<T extends Component> {

private Class<T> classType;

public ComponentMapper(Class<T> type, World world) {
    this.type = ComponentTypeManager.getTypeFor(type);
    this.classType = type;
}

public T get(Entity e) {
    return classType.cast(em.getComponent(e, type));
}
}

Objective-C:

@interface ComponentMapper : Component
{
    ComponentType* componentType;
    EntityManager* entityManager;
    id classType;
}

- (id) init:(id) componentType World:(World*) world;
- (id) get:(Entity*) entity; // can this just return Entity instead

@end

@implementation ComponentMapper 

- (ComponentMapper*) init:(id) type World:(World *) world {
    if (self = [super init]) {
        // should be a call to getEntityManager()
        self->entityManager = [world entityManager];
        self->componentType = [ComponentTypeManager getTypeFor:type];
        self->classType = type;
    }

    return self;
}

- (id) get:(Entity*) entity
{
    return [classType cast:[em getComponent:e param1:type]];
}

@end
Perception :

Java generics were created to add compile-time type safety to collections and parametrized classes. In a statically typed language like Java this can be very useful, but much more less so in Objective C which is dynamically typed and has dynamic dispatch.

I would advise you simply drop generic placeholders when converting from Java, and replace generic return types/arguments with id.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive