I am making a currency converter app and I’m having some issues with the storage of the latest exchange rates. I have a method called applicationDidUpdateCurrency which can be called manually from within the app using a button, or when the app starts by clicking yes to an alert. The method gets the latest exchange rates and adds them to an array. I then have the app loop through a series of statements where the items from the NSArray are moved over to a float value and then logged to the console. All of that works fine.
+(void)applicationDidUpdateCurrency
{
NSError *error;
// Load .csv file.
NSString *allStates = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=USDUSD=X,CNYUSD=X,GBPUSD=X,EGPUSD=X,CADUSD=X,EURUSD=X,&f=sl1d1t1ban&e=.csv"] encoding:NSUTF8StringEncoding error:&error];
// Remove all quotes from file, replace them with nothing.
NSString *strippedPartOne = [allStates stringByReplacingOccurrencesOfString:@"\"" withString:@""];
// Remove excess new lines from the csv file.
NSString *strippedPartTwo = [strippedPartOne stringByReplacingOccurrencesOfString:@"\r" withString:@""];
// Add strippedPartTwo to array.
NSArray *rows = [strippedPartTwo componentsSeparatedByString:@"\n"];
// NSArray *components;
for(int i=0; i<[rows count]; i++)
{
components = [[rows objectAtIndex:i] componentsSeparatedByString:@","];
// NSLog(@"Conversion: %@ Value: %@", [components objectAtIndex:0], [components objectAtIndex:1]);
if ([[rows objectAtIndex:i] isEqualToString:@""]) {
continue;
}
if (i==0) {
float floatUSD = 1;
NSLog(@"USD2: %f", floatUSD);
continue;
}
if (i==1) {
float beforeFloatCNY = [[components objectAtIndex:1] floatValue];
float floatCNY = 1 / beforeFloatCNY;
NSLog(@"CNY2: %f", floatCNY);
}
if (i==2) {
float beforeFloatGBP = [[components objectAtIndex:1] floatValue];
float floatGBP = 1 / beforeFloatGBP;
NSLog(@"GBP2: %f", floatGBP);
}
if (i==3) {
float beforeFloatEGP = [[components objectAtIndex:1] floatValue];
float floatEGP = 1 / beforeFloatEGP;
NSLog(@"EGP2: %f", floatEGP);
}
if (i==4) {
float beforeFloatCAD = [[components objectAtIndex:1] floatValue];
float floatCAD = 1 / beforeFloatCAD;
NSLog(@"CAD2: %f", floatCAD);
}
if (i==5) {
float beforeFloatEUR = [[components objectAtIndex:1] floatValue];
float floatEUR = 1 / beforeFloatEUR;
NSLog(@"EUR2: %f", floatEUR);
}
}
}
This method is in the same file that is associated with a table view controller which has a list of the currencies. I am simply trying to put those values back into that array so that they can more easily be sent back to the main view controller with a delegate.
After the applicationDidUpdateCurrency method has been called I can’t seem to a simple NSLog(@”USD2: %f”, floatUSD); to display that value again. It logs correctly right after it is defined, but even after the next if statement it just shows as 0.000000.
I declare the floatX values in the.h file using this code.
@property (nonatomic, assign) float floatUSD;
Could anyone help me out with this? I’m simply trying to define a float value, put it into an array, and send it back to the main view controller when the item is selected in the table view controller.
Because you’re re-declaring the variables and the newly declared variable (now in the innermost scope) hides the instance variable. (If you compiled this with warnings turned on, you would get some warnings about this). You don’t need, and in fact you should not re-declare these.
Furthermore, access the property itself, and not the backing instance variable. So, all in all, instead of
write