Search code examples
j2objcj2objc-gradle

How to use enum in j2Objc


I define a enum named SexType, and want use as the following:

public class People {
    //@Property("copy, nonatomic") protected String location;
   // @Property("copy, nonatomic") protected SexType sex;
   public String name;
   public SexType sex;
   public People(String name){
     this.name = name;
  }

  public String say(String text){
        return this.name + " say:" + text;
  }

public void setName(String name){
    this.name = name;
}

}

It run ok in java side, but it failed when translate to objC and error is "SexType cannot be resolved to a type". Who know the reason?

Thanks


Solution

  • j2objc is a Java compiler (it uses Eclipse's JDT as its front-end), and requires the same path flags as javac. Since "class cannot be resolved to a type" is a Java compiler error, that means it's likely that the -sourcepath flag is incorrect, since that's how Java compilers locate types in other Java source files.

    The quick way to verify correct source and class path in a j2objc command line is to use those same flags in a javac command, fix any compilation errors, then use those same paths in the j2objc command. To see what command Xcode is failing with, put an "echo " in front of the j2objc command in its build rule, build the app, and then expand the "Run shell script build rule on ..." log entry to see the command.

    The second line of the command log has a "cd " command, open a terminal window and run that line so you are in the working directory Xcode uses. Type javac -sourcepath ../../j2ObjcDemon_SharedLib/src/main and the path to either or both Java sources. Fix the source path until javac can compile those files, then use the fixed path in the j2objc build rule.