Enum access translated using J2objc to Swift

I am using J2objc to translate Java to Objective-C. I use this code with the bridge title to make it available in Swift. Here is the Java Enum I translated:

public enum BTestType {

  Type1, Type2, Type3;

}

In Objective-C, I get the following header file (I skip the module file):

#ifndef _BISBTestType_H_
#define _BISBTestType_H_

#include "J2ObjC_header.h"
#include "java/lang/Enum.h"

typedef NS_ENUM(NSUInteger, BISBTestType) {
  BISBTestType_Type1 = 0,
  BISBTestType_Type2 = 1,
  BISBTestType_Type3 = 2,
};

@interface BISBTestTypeEnum : JavaLangEnum < NSCopying >

#pragma mark Package-Private

+ (IOSObjectArray *)values;
FOUNDATION_EXPORT IOSObjectArray *BISBTestTypeEnum_values();

+ (BISBTestTypeEnum *)valueOfWithNSString:(NSString *)name;
FOUNDATION_EXPORT BISBTestTypeEnum *BISBTestTypeEnum_valueOfWithNSString_(NSString *name);

- (id)copyWithZone:(NSZone *)zone;

@end

J2OBJC_STATIC_INIT(BISBTestTypeEnum)

FOUNDATION_EXPORT BISBTestTypeEnum *BISBTestTypeEnum_values_[];

#define BISBTestTypeEnum_Type1 BISBTestTypeEnum_values_[BISBTestType_Type1]
J2OBJC_ENUM_CONSTANT_GETTER(BISBTestTypeEnum, Type1)

#define BISBTestTypeEnum_Type2 BISBTestTypeEnum_values_[BISBTestType_Type2]
J2OBJC_ENUM_CONSTANT_GETTER(BISBTestTypeEnum, Type2)

#define BISBTestTypeEnum_Type3 BISBTestTypeEnum_values_[BISBTestType_Type3]
J2OBJC_ENUM_CONSTANT_GETTER(BISBTestTypeEnum, Type3)

J2OBJC_TYPE_LITERAL_HEADER(BISBTestTypeEnum)

typedef BISBTestTypeEnum BISTestTypeEnum;

#endif // _BISBTestType_H_

To access the enumeration in Swift, I had to call the following:

 var r:BISBTestTypeEnum = BISBTestTypeEnum.values().objectAtIndex(BISBTestType.Type1.rawValue) as! BISBTestTypeEnum

Is there an easier way to access Objective-C enums in Swift?

+4
source share
3 answers

There is an easier way to do this. Add a flag --static-accessor-methods, and then you can access, for example:

var r:BISBTestTypeEnum = BISBTestTypeEnum.Type1()
+1
source

BISBTestTypeEnum :

extension BISBTestTypeEnum {
    class func withValue(value: BISBTestType) -> BISBTestTypeEnum {
        return BISBTestTypeEnum.values().objectAtIndex(value.rawValue) as! BISBTestTypeEnum
    }
}

:

var r = BISBTestTypeEnum.withValue(BISBTestType.Type1)
0

It looks like you can use the method BISBTestTypeEnum_get_Type2()(obtained from the macro J2OBJC_ENUM_CONSTANT_GETTER(BISBTestTypeEnum, Type2)), but despite the fact that it passes the compilation, it does not work during communication.

-1
source

All Articles