Select Page
Latest 2.0 https://github.com/HiveHicks/HHUnitConverter MIT ios 7, requires ARC PESGraph HiveHicks

Unit conversion library for Objective-C

## Installation

### CocoaPods

You can install `HHUnitConverter` using CocoaPods. Just add the following line to your Podfile:

``pod 'HHUnitConverter'``

### Manual

You can also add `HHUnitConverter` manually by copying files in `Library` folder to your project. Note, however, that the library relies on `PESGraph` which can be found here: https://github.com/snyderp/PESGraph. So, don’t forget to add that to your project as well.

## Usage

Conversions in the library are based on formula y = A * x + B, where x is a source value, y is a target value, A is a multiplier and B is a summand in a conversion rule.

For instance, miles can be converted to kilometers by multiplying x by 1.6. So, in this case A = 1.6 and B = 0. Kelvin value is converted to Celcius by subtracting 273, so for Kelvin-to-Celsius conversion: A = 1, B = -273. To specify the rule you have a generic `setConversionRule:fromUnit:toUnit:` method, but you are more likely to use convenience methods that start with `letUnit:convertToUnit:...`

To use conversion library you first need to create an instance of `HHUnitConverter` and set convertion rules that you need:

``````HHUnitConverter *converter = [HHUnitConverter new];
[converter letUnit:@"mi" convertToUnit:@"km" byMultiplyingBy:1.609344];
[converter letUnit:@"km" convertToUnit:@"m" byMultiplyingBy:1000];
[converter letUnit:@"m" convertToUnit:@"cm" byMultiplyingBy:100];``````

Then you can use converter object to convert values however you like. Converter is clever enough to find out dependencies between units that you’ve registered conversion rules with (as well as backward dependencies), so when you say that miles can be converted to kilometers, and kilometers can be converted to meters, it knows how to convert miles to meters correctly (and vice versa), so it’s OK to have a call like that:

``[converter value:482803.2 convertedFromUnit:@"m" toUnit:@"mi"]``

Of course, it will handle the simplest cases like that:

``[converter value:300 convertedFromUnit:@"mi" toUnit:@"km"]``

Also, the library can handle compound units conversion, so, for instance, you can convert "litres per kilometer" to "gallons per mile" using code like that:

``````[converter letUnit:@"mi" convertToUnit:@"km" byMultiplyingBy:1.609344];
[converter letUnit:@"gal" convertToUnit:@"L" byMultiplyingBy:3.78541178];
[converter value:20 convertedFromUnit:@"L/km" toUnit:@"gal/mi"];``````

### Latest podspec

```{
"name": "HHUnitConverter",
"version": "2.0",
"summary": "Unit conversion library for Objective-C.",
"homepage": "https://github.com/HiveHicks/HHUnitConverter",
"type": "MIT",
},
"authors": {
"HiveHicks": "[email protected]"
},
"source": {
"git": "https://github.com/HiveHicks/HHUnitConverter.git",
"tag": "2.0"
},
"platforms": {
"ios": 7
},
"source_files": "Library/**/*.{h,m}",
"requires_arc": true,
"dependencies": {
"PESGraph": []
}
}```