Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an enum with constructor in Swift like Java?

Tags:

java

swift

For instance, we have the Java code like following;

enum Job {
    NINJA(3000000){
        public void attack() {
            //use Shuriken
        }
    },
    SAMURAI(4000000){
        public void attack() {
            //use Sword
        }
    };
    public final int salary;
    public abstract void attack();
    private Job(int salary) {
        this.salary = salary;
    }
}

In Swift, I don't think we can define a constructor and have any methods of enum.

I found out we can have a similar structure in the following Swift code, but cannot have any methods.

class Job {
    class Store {
        let salary : Int
        init(int salary) {
            self.salary = salary
        }
    }
    class var NINJA: Store{
        return Store(3000000)
    }
    class var SAMURAI: Store{
        return Store(4000000)
    }
}
// Job.NINJA.salary

Of course, I know Swift enum can have their own properties.

But if you have more properties in the following case, we must have so many switch at each properties. I think it's not smart.

enum Job {
    var salary: Int{
        switch self{
        case NINJA:
            return 3000000
        case SAMURAI:
            return 4000000
        }
    }
    case NINJA
    case SAMURAI
}

So, if you were me, how do you write your Swift code in this case?

like image 560
Mitsuaki Ishimoto Avatar asked Jul 17 '14 11:07

Mitsuaki Ishimoto


People also ask

Can enum have constructor Java?

Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

Can you use enum in constructor?

The constructor takes a string value as a parameter and assigns value to the variable pizzaSize . Since the constructor is private , we cannot access it from outside the class. However, we can use enum constants to call the constructor.

How do I enum in Swift?

In Swift, we can also assign values to each enum case. For example, enum Size : Int { case small = 10 case medium = 12 ... } Here, we have assigned values 29 and 31 to enum cases small and medium respectively.

Can enum type can define constructor?

Enum FieldsThe enum constructor sets the int field. When the constant enum values are defined, an int value is passed to the enum constructor. The enum constructor must be private . You cannot use public or protected constructors for a Java enum .


1 Answers

Example using struct in swift:

struct MyEnum {

    static let TYPE1 = MyEnum(id: 1, text: "type 1")
    static let TYPE2 = MyEnum(id: 2, text: "type 2")

    let id: Int
    let text : String

    init(id:Int, text:String) {
        self.id = id
        self.text = text
    }

    func getId()->Int {
        return id;
    }

    func getText() ->String {
        return text;
    }

    static func values()->Array<MyEnum> {
        return [TYPE1,TYPE2]
    }

}

func == (left: MyEnum, right: MyEnum) -> Bool {
    return left.id == right.id
}

Usage example:

for e in MyEnum.values() {
    println(String(e.getId()) + " " + e.getText())
}

let type1 = MyEnum.TYPE1
if (type1==MyEnum.TYPE1) {
    println("is Type 1")
} else {
    println("is not Type 1")
}

Console output

1 type 1
2 type 2
is Type 1
like image 143
Dan Haggren Avatar answered Oct 05 '22 06:10

Dan Haggren