See the codes below:
enum Gender {
case Male, Female, Unknown
}
enum ExplicitGender {
case Male, Female
}
func whatGender(_ g: Gender) {
print("I am \(g)")
}
func makeSureYourGender() -> ExplicitGender {
return ExplicitGender.Male
}
var myGender = makeSureYourGender();
// How to Cast type `ExplicitGender` to `Gender`?
// This fails
// whatGender(myGender)
The syntax myGender as ExplicitGender is not correct.
Actually, the Male,Female in ExplicitGender and Gender are the same.
Make the 2 is 2 different Enum Types is not exactly correct.
Or There is another code structure or syntax to resolve this problem?
See also:
enum Language {
case English, French, Arabic, Russian, Chinese, Spanish, Japanese, German, Italian
}
enum UNOfficialLanguage {
case English, French, Arabic, Russian, Chinese, Spanish
}
In typescript, i will do it with:
type UNOfficialLanguage = 'English' | 'French' | 'Arabic' | 'Russian' | 'Chinese' | 'Spanish';
type Language = UNOfficialLanguage | 'Japanese' | 'German' | 'Italian';
Your TypeScript example is not an enum. It's a union of string literal types. There is no equivalent to literal types in Swift. (TypeScript also has enums, but they're a different thing.)
Swift and TypeScript have radically different approaches to what it means to be a type. In TypeScript, two things that have the same structure are the same type ("structural typing"). In Swift, two things can have the same structure can be unrelated and what matters is the name of the type ("nominal typing"). The TS way is convenient, but there are many types it cannot express. The Swift way can express more things, but it lacks certain conveniences.
For example, in TypeScript, if you declare two interfaces with the same properties, they are interchangeable:
The equivalent code in Swift will give an error:
Whether you think one approach is better typically depends on the problems you're trying to solve. (I find the Swift approach dramatically more powerful than the TypeScript approach. Others find Swift restrictive because it won't let you do things you "know" are correct unless you can prove to the compiler it's correct.)
To your example, just because two symbols have the same spelling (
Language.EnglishandUNOfficialLanguage.English) doesn't mean they are related in any way. If you want to convert between them, you'll need to convert between them.That said, Swift does have conveniences to convert to strings and from strings.
Note the addition of
: String. This allows you to convert easily between the enum and a string representation of the enum.Note that
languageis of typeLanguage?since this conversion could fail. Since you happen to know that Language is a superset of UNOfficialLanguage, you could force the conversion in that direction:For more on this feature, see Raw Values in the Swift Programming Language.