HealthKit composite data for HealthKit development Tutorials
Composite data is data composed of composite units and values. The so-called compound unit is by the unit multiplication, division, etc. to get units, such as M/s, lb ft, etc. is a composite unit. This section will explain these composite data.
Compound data obtained by multiplying the HealthKit
In physics we may have been exposed to similar lb ft (torque units, torque is a special moment that causes the object to rotate.) ) such a unit. This unit can be seen as a compound unit multiplied by two units. Developers who want to use this compound unit in their own programs first need to create this unit. Creating such a unit requires the use of the Unitmultipliedbyunit (_:) method, which has the following syntax:
- Func Unitmultipliedbyunit (_ unit:hkunit!), hkunit!
Where the parameter unit: is used to specify the units to be multiplied.
The torque of the generator in the car is shown under "Example 2-44:hkunit-unitmultipliedbyunit" in "points. Feet" units. The code is as follows:
- Import UIKit
- Import HealthKit
- Class Viewcontroller:uiviewcontroller {
- Override Func Viewdidload () {
- Super.viewdidload ()
- Additional setup after loading the view, typically from a nib.
- var footunit = Hkunit.footunit () Create a length unit
- var poundunit=hkunit.poundunit ()//Create quality Unit
- var unitmultipliedbyunit=footunit.unitmultipliedbyunit (poundunit) //Multiply the resulting compound units
- var myforce=hkquantity (Unit:unitmultipliedbyunit, doublevalue:100)
- println ("The torque of the generator in the car is: \ (myforce)")
- }
- ......
- }
When you run the program, you see the following effect.
- The torque of the generator in the car is: lb ft
The compound data obtained by dividing the HealthKit
In mathematical calculations, we often encounter units such as M/S. This unit is divided into two units. If the developer wants to use this unit in their own code. You first need to use the Unitdividedbyunit (_:) method to create this unit, with the following syntax:
- Func Unitdividedbyunit (_ unit:hkunit!), hkunit!
Where the parameter _ unit is used to specify units, this unit is used as a divisor.
The "Example 2-45:hkunit-unitdividedbyunit" below shows the speed of the user running, in meters per second. The code is as follows:
- Import UIKit
- Import HealthKit
- Class Viewcontroller:uiviewcontroller {
- Override Func Viewdidload () {
- Super.viewdidload ()
- Additional setup after loading the view, typically from a nib.
- Let meters = Hkunit.meterunit ()//Create length units
- Let seconds = hkunit.secondunit ()//creation time unit
- Let Meterspersecond = meters.unitdividedbyunit (seconds)// divide the resulting compound units
- Let Speed=hkquantity (Unit:meterspersecond, Doublevalue:2)
- println ("Xiao Ming runs at speed: \")
- }
- ......
- }
When you run the program, you see the following effect.
- Xiao Ming runs at a speed of: 2 m/s
Compound data consisting of the HealthKit of units and integers
We use units such as M3 and m2 when we are seeking volume and area. This unit is a composite unit composed of units and the sub-party of integers. If developers want to create such a unit, they need to use the Unitraisedtopower (_:) method, which has the following syntax:
- Func Unitraisedtopower (_ Power:int), hkunit!
Where the parameter power is an integer used to specify the secondary side.
The volume of the pool is shown under "Example 2-46:hkunit-unitraisedtopower" in "cubic meters". The code is as follows:
- Import UIKit
- Import HealthKit
- Class Viewcontroller:uiviewcontroller {
- Override Func Viewdidload () {
- Super.viewdidload ()
- Additional setup after loading the view, typically from a nib.
- var meters = Hkunit.meterunit ()
- var cubicmeter=meters.unitraisedtopower (3)// Unit and integer composition unit of the sub-square
- var pool=hkquantity (Unit:cubicmeter, doublevalue:100)
- println ("Pool size: \ (pool)")
- }
- ......
- }
When you run the program, you see the following effect.
- The volume of the pool is: m^3
A compound unit of the reciprocal composition of the HealthKit
1/S This unit is a "Hertz" unit, which represents the frequency of a cycle process occurring within a 1-second interval. A unit similar to 1/s is composed of the reciprocal of a unit. If you want to use this unit in your program, you need to use the Reciprocalunit () method. The function of the Reciprocalunit () method is to construct a new compound unit by the inverse of the unit. Its grammatical form is as follows:
- Func reciprocalunit ()-hkunit!
"Example 2-47:hkunit-reciprocalunit" shows the frequency of a periodic process occurring within a 1-second interval, in units of "1/second". The code is as follows:
- Import UIKit
- Import HealthKit
- Class Viewcontroller:uiviewcontroller {
- Override Func Viewdidload () {
- Super.viewdidload ()
- Additional setup after loading the view, typically from a nib.
- var seconds = Hkunit.secondunit ()
- var secondsinverse = seconds.reciprocalunit () //compound unit composed of reciprocal
- var frequency=hkquantity (Unit:secondsinverse, doublevalue:50)
- println ("The frequency of a cycle process occurring within a 1-second interval is: \ (frequency)")
- }
- ......
- }
When you run the program, you see the following effect.
- The frequency at which a cycle process occurs within a 1-second interval is: 1/s
This article is selected from: HealthKit Development Quick Start Tutorial University PA information, reproduced Please indicate the source, respect the technology respect the IT person!
HealthKit composite data for HealthKit development tutorials