Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum: Count and list all cases (from outside!)

Tags:

enums

ios

swift

I've got this enum (Xcode 10/Swift 5) in its own file ("MyEnum.swift"):

enum MyEnum: Int, CaseIterable {
    case A = 0
    case B = 1
    case C = 2
    case D = 3
    case E = 4
    //Each case needs its own number and description!

    var description: String {
        switch self {
            case .A:
                return "abc"
            case .B:
                return "bcd"
            case .C:
                return "cde"
            case .D:
                return "def"
            case .E:
                return "efg"
        }
    }
}

... and want to add the descriptions to a PickerView. I know how to set up the functions for the view but I'm now stuck on counting the enum cases and adding the descriptions.

According to the documentation and different questions, adding CaseIterable is supposed to make it possible to call

MyEnum.allCases.count

... but I can only access allCases from within the enum file. From outside (so from my ViewController class) I can only call MyEnum.AllCases, which hasn't got count and, to be honest, I'm not even sure what AllCases returns exactly (it's not a normal array you can use count on).

Adding this code (source) to the enum file at least makes it possible to count the cases:

static let count: Int = {
    var max: Int = 0
    while MyEnum(rawValue: max) != .none { max += 1 }
    return max
}()

...but isn't there supposed to be an easier way to do this with Swift 4.2+?

How do I get a list of the case descriptions to fill my PickerView with (so "abc", "bcd",... - preferably without hardcoding it)?

like image 565
Neph Avatar asked Apr 12 '19 10:04

Neph


People also ask

What does the CaseIterable protocol do?

CaseIterable Protocol This follows the pattern of Equatable, Comparable and Hashable by having the compiler automatically synthesize the implementation. The compiler then provides you an allCases that is a collection of all cases of the enum: Direction.

Can enum conform to Swift protocol?

Yes, enums can conform protocols. You can use Swift's own protocols or custom protocols. By using protocols with Enums you can add more capabilities.

How to make enum CaseIterable Swift?

You can iterate over all of the possible cases of an enumeration in Swift by making it conform to the CaseIterable protocol. When using CaseIterable, you can access a collection of all enum cases by using a allCases property and then iterate over it as an array.

What is the raw underlying type of this enum?

What is raw underlying type of enum? Enum with Raw Values Raw values can be strings, characters, or any of the integer or floating-point number types. Each raw value must be unique within its enumeration declaration.


2 Answers

I tried in 1 file:

enum TestEnum: CaseIterable {
    case test1
    case test2
}

in another file of another class I wrote:

let count = TestEnum.allCases.count

And it works, but I noticed that when I was typing "allCases" wasn't shown

enter image description here

I manually needed to write it

like image 86
Paul T. Avatar answered Oct 12 '22 20:10

Paul T.


allCases is declared public, so you should be able to access it from another file:

/// A collection of all values of this type.
public static var allCases: Self.AllCases { get }

like this:

let descriptions = MyEnum.allCases.map { $0.description }

Try cleaning the project and rebuild.

Also, if something is not in Xcode's autocomplete list, it doesn't mean that you can't access that thing. Xcode's complete list has a ton of bugs according to my own experience.

like image 39
Sweeper Avatar answered Oct 12 '22 19:10

Sweeper