I was wondering how, in objective c, one would go about making a unit converter, in particular one specific point: would you need a new block of code for each unit to be converted to every other unit. e.g.
meters – feet
meters – centimeters
meters – inches
etc then have a new code to convert feet to each of the units, then one for inches to each, another for centimetres to each unit etc, or is there some easier way?
Thanks
In general all units conversions are of the same form:
Boften even being zero (meters to feets, inches to meters, etc) — and being non-zero only for a some cases (like converting temperature from Kelvins to Celcius)What you only need is to keep a list of the values of A and B for each pair of units to convert one unit to another.
Note that you can even keep only main values (from metric system units to other units for example) and deduce the others, as from the formula above you can obviously conclude the reverse
So once you have the list of values A and B for every pair of units you want to convert from and to, you will only have one method to convert them all.
You can store the list of values A and B whereever you want, in a dedicated PLIST file, in a sqlite3 database, in CoreData, … (for your case using a PLIST file will probably be much more easier. e.g. a NSDictionary whose keys are the name of the origin unit, and values are themselves NSDictionaries whose keys are destination units and values are the A and B pairs… — that’s just a suggestion, not the only solution)