简体   繁体   中英

Assistance with converting Java to Objective-C

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

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

Nope, ComponentMapper<T extends Component> means the class ComponentMapper can be parameterized with any class that extends Component (or Component itself). Thus you could do new ComponentMapper<MySpecialComponent> etc.

At runtime that generic information is erased resulting in a ComponentMapper<Component> ), ie the compiler would see public T get(Entity e) as public MySpecialComponent get(Entity e) whereas after type erasure at runtime it is just public Component get(Entity e) .

Besides that ComponentWrapper and Component have a has-a relationship, ie ComponentWrapper has a member of type Component.class (or Class<Component> , which means the same: not a Component instance but a reference to the class itself) . Extending would be a is-a relation.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM