Build an array of UnitMass values to present to UI/user as a list of unit options to measure weight

Problem: Present an array of

UnitMass
values, to present to my app user in a simple table view, to facilitate a user selection.


The intention is that this

selectedUnit
could then be used in conjunction with the
Measurement
framework, in the following manner:
let measureWeight = Measurement(value: tempMeasure.value, unit: selectedUnit)


Essentially I want to build an array of available UnitMass... for example:

let arrayUnitMass: [UnitMass] = [.carats, .centigrams, .decigrams, .grams, .kilograms, etc...]


But I want to do this dynamically and from the

UnitMass
units available in Foundation.


I could write an extension on

UnitMass
to conform to
CaseIterable
, but then, as far as I understand, I'd have to implement my own definition of
.allCases
. (Based on "Enumerating enum cases in Swift", an article written by Ole Bergemann).


So maybe I'd be better off writing my own

Enum
for
UnitMass
...
enum SelectUnitMass {
    case carats, centigrams, decigrams, grams, kilograms//, etc...
}


Frankly I'm too lazy to maintain this

enum
myself and so, as mentioned above, I want to do this dynamically from the
UnitMass
units available in Foundation. The benefit for this choice will IMHO ensure parity with any future additions (but maybe not deletions).


This SO "Get all enum values as an array" is informative, however it does not seem to provide any hints to a solution.


Are you aware of some way to prepare an array of all

UnitMass
units available in Foundation?

Replies

I did not find a solution.


But this seems to point in a direction that could help do it without CaseIretable:

h ttps://medium.com/@londeix/listing-all-cases-in-an-enum-3b057f2c1432