Is there something like renaming Java to Objective-C?

I have a situation in Objective-C where a Java-style enumeration will be very useful. I have a set of layer types, and each level has its own save value (stored in seconds). I want to keep these types of layers in an enumeration, since they are too similar to separate classes, for example:

typedef enum { ExplosionLayerType, FireworkLayerType, FireLayerType, FireJetLayerType } FXLayerType; 

In Java, I could easily associate these two values ​​with something like this:

 public enum FXLayerType { Explosion(3), Firework(6), Fire(7), FireJet(-1); private int persistence; FXLayerType(int persistence) { this.persistence = persistence; } } 

Is there an easy way to create a kind of lightweight class like this in Objective-C, or do you have to resort to more primitive methods?

EDIT:

Various people have suggested doing something like this:

 typedef enum { ExplosionLayerType = 3, FireworkLayerType = 6 } FXLayerType; 

This will not work for me, as I may have something like this (Java style listing):

 Explosion(3), Firework(6), Dust(3); 

In Java, Dust and Explosion will be treated as unique values, but a direct assignment using C enumerations will treat them as one and the same.

+4
source share
6 answers

If you just want a primitive container for type and value, consider this approach:

 typedef struct FXLayerValue { FXLayerType type; int value; } FXLayerValue; 

Again, the class hierarchy may be worth considering if things get complicated or are better handled dynamically. Caution: if you have a ton of objects to save and / or create, the objc type will be excessive and degrade performance.

Unfortunately, my Java-Fu is not good enough to know all the language differences for listings.

+2
source

To emulate a Java enumeration, we need something comparable (there may be operands == , etc.), which can have fields and are lightweight. This involves structures, and possibly pointers to structures. Here is an example of the latter:

FXLayerType.h:

 typedef const struct { int persistence; } FXLayerType; extern FXLayerType * const LayerTypeExplosion; extern FXLayerType * const LayerTypeFirework; extern FXLayerType * const LayerTypeDust; 

FXLayerType.m:

 #import "FXLayerType.h" const FXLayerType _LayerTypeExplosion = { 3 }; const FXLayerType _LayerTypeFirework = { 6 }; const FXLayerType _LayerTypeDust = { 3 }; FXLayerType * const LayerTypeExplosion = &_LayerTypeExplosion; FXLayerType * const LayerTypeFirework = &_LayerTypeFirework; FXLayerType * const LayerTypeDust = &_LayerTypeDust; 

So, FXLayerType is a struct constant, whereas, as in Obj-C objects, we always use pointers to these structures. The implementation creates 3 constant structures and 3 constant pointers to them.

Now we can write the code, for example:

 FXLayerType *a, *b; a = LayerTypeDust; b = LayerTypeExplosion; NSLog(@"%d, %d\n", a == b, a->persistence == b->persistence); 

Which will output "0, 1" - a and b will be different enumerations (0), but will have the same stability (1). Note that here a and b are not constant pointers, only enumerated "literals" are defined as constants.

As indicated, this has the disadvantage that you cannot switch by enumeration value. However, if necessary, just add a second field, say tag , and run it with a unique value using a real enumeration, say FXLayerStyleTag . You can also remove indirect access if you are always happy to compare tags (for example, a.tag == b.tag`). This gives you:

FXLayerType.h:

 typedef enum { ExplosionTag, FireworkTag, DustTag } FXLayerTypeTag; typedef struct { FXLayerTypeTag tag; int persistence; } FXLayerType; extern const FXLayerType LayerTypeExplosion; extern const FXLayerType LayerTypeFirework; extern const FXLayerType LayerTypeDust; 

FXLayerType.m:

 #import "FXLayerType.h" const FXLayerType LayerTypeExplosion = { ExplosionTag, 3 }; const FXLayerType LayerTypeFirework = { FireworkTag, 6 }; const FXLayerType LayerTypeDust = { DustTag, 3 }; 

Using:

 FXLayerType a, b; a = LayerTypeDust; b = LayerTypeExplosion; NSLog(@"%d, %d\n", a.tag == b.tag, a.persistence == b.persistence); 

The difference between the two projects is the first passes around the pointers, and the second structures, which may be larger. You can combine them to get switch capable pointer-based enumerations - this will remain as an exercise!

Both of these projects also have the advantage (dis) that the number of "literals" of an enumeration can be expanded at any time.

+2
source

In fact, you can assign values ​​to enumeration keys in C since they are nothing but ints:

 typedef enum { LayerTypeExplosion = 3, LayerTypeFirework = 6, LayerTypeFire = 7, LayerTypeFireJet = -1 } FXLayerType; 

You can use them then simply as a limited set of values ​​that should be assigned to a variable of type FXLayerType .

 FXLayerType myLayerType = LayerTypeFirework; NSLog(@"Value of myLayerType = %i", myLayerType); // => "Value of myLayerType = 6" 
+1
source

This is not the equivalent of 100%, but it may be an approach that you can use in Objective-C. Basically, create some handy class-level methods to build various FXLayerType configurations.

 @interface FXLayerType { @private int persistence; } + (FXLayerType*)fireworkLayerType; + (FXLayerType*)explosionLayerType; + (FXLayerType*)jetLayerType; @end @implementation FXLayerType + (FXLayerType*)explosionLayerTypeWithPersistence:(int)persistence { FXLayerType* layerType = [[FXLayerType new] autorelease]; layerType->persistence = persistence; return layerType; } + (FXLayerType*)explosionLayerType { return [self explosionLayerTypeWithPersistence:3]; } + (FXLayerType*)fireworkLayerType { return [self explosionLayerTypeWithPersistence:6]; } + (FXLayerType*)jetLayerType { return [self explosionLayerTypeWithPersistence:-1]; } @end 

Using:

 FXLayerType* aJetLayerType = [FXLayerType jetLayerType]; 
0
source

I recently used the j2objc format for listings. It works pretty nicely. In addition, you can automatically generate your enumerations if you are trying to translate a map directly from a java object.

https://code.google.com/p/j2objc/wiki/Enums

However, I removed the specific j2objc classes from my "Enums". I do not need additional dependencies.

0
source

Structure-based answers look good on their face, but crash when trying to add Objective-C objects to the structure. Given this limitation, a true imitation of Java-style renaming can be more work than it costs.

0
source

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


All Articles