Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to pass an enum type name as an argument in Swift?

Tags:

types

enums

swift

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?

like image 735
Kevin Avatar asked Aug 05 '16 16:08

Kevin


People also ask

Can you use an enum as a type?

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');

Can we inherit enum in Swift?

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.

Is enum value type in Swift?

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 .

Can different enumeration may have same name?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.


1 Answers

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"
like image 114
Hamish Avatar answered Oct 09 '22 23:10

Hamish