Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public enum with Internal raw value in Swift

I want to create a class with a public enum (to be used by other modules) with an internal String raw value (I don't want other modules to read the raw Value, but I do want to read it internally inside the same module).

I know I can just create another private function that receives the enum and returns a String but I want to avoid that.

like image 741
YogevSitton Avatar asked Nov 10 '22 01:11

YogevSitton


1 Answers

I think it's not possible, I tried doing like this first:

public enum Emotion {
    case Hate = EmotionInner.Hate, Love = EmotionInner.Love
}
internal enum EmotionInner: String {
    case Hate = "hate", Love = "love"
}

But it won't compile. Your enums at least should be the same type - so if you want to has rawValue() it will has it in both enum. And you can't make rawValue() smth like final or static.

By the way, maybe it's not bad way to make a function for it?

like image 197
katleta3000 Avatar answered Nov 15 '22 05:11

katleta3000