Currency conversion in Objective-C
How can I convert between currencies in Objective-C, using either the Cocoa or Coc开发者_JS百科oa Touch frameworks?
Well, aren't you a lucky fellow:
https://github.com/davedelong/DDUnitConverter
Simply copy the entire "DDUnitConverter" subfolder into your project, then do:
#import "DDUnitConverter.h"
#import "DDCurrencyUnitConverter.h"
- (void) convertCurrency {
DDUnitConverter *converter = [DDUnitConverter currencyUnitConverter];
NSNumber *from = [NSNumber numberWithInt:42];
NSNumber *to = [converter convertNumber:from fromUnit:DDCurrencyUnitUKPoundSterling toUnit:DDCurrencyUnitUSDollar];
NSLog(@"new value: %@", to);
}
That will convert £42 to USD$.
Rates are pulled from the IMF automatically, but if you really need to, you can invoke -refreshExchangeRates
on your unit converter instance.
The -convertNumber:fromUnit:toUnit:
call will block until the rates can be downloaded and parsed. Subsequent calls will just use the already-downloaded exchange rates.
Actually I've choose:
https://github.com/davedelong/DDUnitConverter
Rates are pulled from the IMF but during the conversion , there are problems using NSDecimalNumber functions ( decimalNumberByMultiplyingBy and decimalNumberByDividingBy: they dont give the correct results if you dont use NSDecimalNumber var's created by strings), so I've correct the conversion method like this and now it work:
DDUnitConverter.m
- (NSNumber *)convertNumber:(NSNumber *)number fromUnit:(DDUnit)fromUnit toUnit:(DDUnit)toUnit {
if (fromUnit == toUnit) { return number; }
//make sure it's an NSDecimalNumber
NSDecimalNumber *source = [NSDecimalNumber decimalNumberWithDecimal:[number decimalValue]];
NSDecimalNumber *convertToBaseUnit = [[self class] multiplierForUnit:fromUnit];
NSDecimalNumber * n1 = [NSDecimalNumber decimalNumberWithString:[source stringValue]];
NSDecimalNumber * n2 = [NSDecimalNumber decimalNumberWithString:[convertToBaseUnit stringValue]];
NSDecimalNumber *valueInBaseUnit = [n1 decimalNumberByMultiplyingBy:n2];
NSDecimalNumber *convertFromBaseUnit = [[self class] multiplierForUnit:toUnit];
if ([convertFromBaseUnit isEqual:[NSDecimalNumber zero]]) {
convertFromBaseUnit = [NSDecimalNumber one];
}
NSDecimalNumber * n3 = [NSDecimalNumber decimalNumberWithString:[valueInBaseUnit stringValue]];
NSDecimalNumber * n4 = [NSDecimalNumber decimalNumberWithString:[convertFromBaseUnit stringValue]];
NSDecimalNumber *valueInTargetUnit = [n3 decimalNumberByDividingBy:n4];
return valueInTargetUnit;
}
One alternative, is to use the YQL (Yahoo Query Language) to retrieve the latest rates. Yahoo's dev site makes it pretty easy to get started in Objective-C and there are plenty of SO examples on YQL. http://developer.yahoo.com/social/sdk/objectivec/
You may find Yahoo is very nice and liberal with their terms & conditions for many of their data APIs. It's also a lot less fragile than a non dedicated API like Dave is using. Dave writes great code, but the IMF TSV file is not nearly as guaranteed to be available as the YQL services.
精彩评论