Help convert Java to Objective-C

I am trying to convert some code to Objective-C from Java, and I am getting stiff when converting parts of Java Generic. For example, declaring a class in Java, I believe, says that the current class extends any class that is a subclass of the Component class, in Objective-C, does this mean that you are simply extending the Component class?

Any help anyone can provide will be greatly appreciated. Moving forward with this will help me convert other parts 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 
+4
source share
2 answers

Java generators were created to add compilation type security to collections and parameterized classes. In a statically typed language such as Java, this can be very useful, but much less important in Objective-C, which is dynamically typed and has dynamic dispatch.

I would advise you to simply discard generic placeholders when converting from Java and replace the generic return types / arguments with an identifier.

+2
source

For example, declaring a class in Java, I believe, says that the current class extends any class that is a subclass of the Component class

No, ComponentMapper<T extends Component> means that the ComponentMapper class can be parameterized by any class that extends Component (or Component itself). So you can make new ComponentMapper<MySpecialComponent> etc.

At runtime, this general information is deleted, resulting in ComponentMapper<Component> ), that is, the compiler will see public T get(Entity e) as public MySpecialComponent get(Entity e) , whereas after deleting styles at runtime it is just public Component get(Entity e) .

In addition, ComponentWrapper and Component have a has-a relationship, that is, ComponentWrapper has a member of type Component.class (or Class<Component> , which means the same: not an instance of Component , but a reference to the class itself). An extension would be an is-a relationship.

+2
source

Source: https://habr.com/ru/post/1388674/


All Articles