Saturday, April 27, 2024
HomeiOS Developmentios - Customized items in HKUnit for easy measurements like mass, size,...

ios – Customized items in HKUnit for easy measurements like mass, size, and quantity


The purpose of HealthKit sharing is to permit completely different apps to have the ability to contribute and make use of well being knowledge. For this to work, the information recorded within the HealthKit retailer must be in frequent items.

For those who had been to document a distance in Smoots then that will meaningless to another app that learn the information.

Displaying knowledge in your app is a distinct story. You possibly can outline your personal UnitLength for Smoot and use that to transform Meters to Smoots for show or convert entered Smoots to Meters for storage:

extension UnitLength {
    static var smoot: UnitLength = {
        let converter = UnitConverterLinear(coefficient: 1.7)
        return UnitLength(image: "Smoot", converter: converter)
    }()
}

let distance = Measurement(worth: 2, unit: UnitLength.meters)

let smoots = distance.transformed(to: UnitLength.smoot)
print(smoots)

let meters = smoots.transformed(to: UnitLength.meters)
print(meters)

This provides the output:

1.1764705882352942 Smoot

2.0 m

The quick reply is you may’t retailer customized items in HealthKit, however you need not – You deal with them with enter/output, not storage.

You can not create your personal HKUnit – From the documentation

Utilizing Models

Like many HealthKit courses, the HKUnit class isn’t extendable and shouldn’t be subclassed.

Utilizing the UnitLength extension above you can create some comfort capabilities to transform to and from HKQuantity for you

extension HKQuantity {
    
    static func smootQuantity(_ smoots: Double) -> HKQuantity {
        HKQuantity(unit: .meter(), doubleValue: Measurement(worth: smoots, unit: UnitLength.smoot).transformed(to: .meters).worth)
    }
    
    func smootValue()->Double {
        Measurement(worth:self.doubleValue(for: .meter()), unit: UnitLength.meters).transformed(to: .smoot).worth
    }
}

let amount = HKQuantity.smootQuantity(1.0)
let worth = amount.smootValue()



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments