I am trying to figure out if it's possible to pass around enum's type in the same way that you can pass Class
objects in Swift.
My actual use case is a bit more complicated than this, but for discussion, let's say I have two Int
enums:
enum Foo: Int, CustomStringConvertible {
case firstFoo = 0
case anotherFoo = 1
var description: String {
switch self {
case .firstFoo:
return "Hello Foo"
case .anotherFoo:
return "Goodbye Foo"
}
}
}
enum Bar: Int, CustomStringConvertible {
case firstBar = 0
case anotherBar = 1
var description: String {
switch self {
case . firstBar:
return "Hello Bar"
case . anotherBar:
return "Goodbye Bar"
}
}
}
I would like to be able to write a function like this:
func justAnExample(whichEnum: enum) {
let val = whichEnum(rawValue: 0)
print("description: \(String(val))")
}
And then use it like this:
justAnExample(Foo)
// prints: "description: Hello Foo"
justAnExample(Bar)
// prints: "description: Hello Bar"
Is this possible? If so, what is the signature of whichEnum
in the function declaration?
Just like other data types in TypeScript, we can use enums as function parameters or return types, like this: enum Weekend { Friday = 1, Saturday, Sunday } function getDate(Day: string): Weekend { if ( Day === 'TGIF') { return Weekend.Friday; } } let DayType: Weekend = getDate('TGIF');
In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance, Enum and Struct don't.
In Swift there are two categories of types: value types and reference types. A value type instance keeps a unique copy of its data, for example, a struct or an enum . A reference type, shares a single copy of its data, and the type is usually a class .
Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.
You can take advantage of the fact that enums with raw values automatically conform to the RawRepresentable
protocol. You can define a generic function that takes a metatype argument of a given type T
(spelled as T.Type
), where that T
is RawRepresentable
and, also in your case, its RawValue
is Int
.
This will allow you to pass in the metatypes of both Foo
and Bar
, spelled Foo.self
and Bar.self
respectively:
func justAnExample<T : RawRepresentable>(_ enumType: T.Type) where T.RawValue == Int {
// Note that an explicit use of init is required when creating an instance from a
// metatype. We're also using a guard, as `init?(rawValue:)` is failable.
guard let val = enumType.init(rawValue: 0) else { return }
print("description: \(val)")
}
justAnExample(Foo.self) // prints: "description: Hello Foo"
justAnExample(Bar.self) // prints: "description: Hello Bar"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With