Reputation: 69
I'm making a health app, and thought it would be nice to create some custom HKUnit to represent some data by extending HKUnit, but HealthKit documentation says we shouldn't extend or subclass it, so I went to Measurements (Dimension) to try creating a custom Unit.
Basically, creating a custom unit generally means to use a basic unit type (unitduration, length, mass, energy, etc) and a coefficient (converter). But how about when there isn't a type compatible with this unit?
Example: I want to create a BMI unit (kg/m^2 or equivalent) and BMR (Kcal/day), etc. So maybe a MetabolicUnit class with class vars such as bmi, bmr, etc ... As for unit, hopefully using the dividedBy and multipliedBy to get translated units automatically.
Any advice, good practices or already solved code? Being a fairly new framework with a too common name, it's hard to find anything meaningful. Thanks
Upvotes: 0
Views: 651
Reputation: 7363
You can create an instance of HKUnit
that represents BMI without subclassing or extending HKUnit
. Here are two examples of how:
let bmiUnit = HKUnit(from: "kg/m^2")
Or
let meter = HKUnit.meter()
let bmiUnit = HKUnit.gramUnit(with: .kilo).unitDivided(by: meter).unitDivided(by: meter)
Upvotes: 2