好的,我的问题非常简单。下面是我的代码,它从API接收价格,在我的代码中我想要显示今天的当前价格,昨天的价格,以及从昨天到今天的百分比变化。不幸的是,只有当前价格显示正常,但昨天和百分比变化只显示N / A.
感谢任何帮助!
if (refreshing) return;
refreshing = YES;
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
//self.priceLabel.text = @"Loading...";
//self.yesterdayLabel.text = @"Loading...";
//self.changeLabel.text = @"Loading...";
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://coinbase.com/api/v1/prices/spot_rate"]] queue:[NSOperationQueue mainQueue] completionHandler:^ (NSURLResponse *req, NSData *recv, NSError *err) {
if (err) {
// report
[self setCurrentPrice:@"N/A"];
refreshing = NO;
return;
}
NSError *newError = nil;
NSDictionary *serial = [NSJSONSerialization JSONObjectWithData:recv options:0 error:&newError];
if (newError) {
// report
[self setCurrentPrice:@"N/A"];
refreshing = NO;
return;
}
NSString *amount = [serial objectForKey:@"amount"];
NSString *price = [NSString stringWithFormat:@"%@ %@", amount, [serial objectForKey:@"currency"]];
[self setCurrentPrice:[NSString stringWithFormat:@"%@", price]];
// maybe setup a better method.
float diff = [yesterdaysPrice floatValue];
if (diff == 0.0)
self.differenceInPrices = @"N/A";
else {
float amt = [amount floatValue];
float percentChange = amt/diff;
if ((diff == 0) || (amt == diff))
percentChange = 0.00;
percentChange *= 100.00;
self.differenceInPrices = [NSString stringWithFormat:@"%f%%", percentChange];
}
NSMutableDictionary *saveState = [[[NSUserDefaults standardUserDefaults] objectForKey:@"save"] mutableCopy];
[saveState setObject:price forKey:@"price"];
[saveState setObject:yesterdaysPrice forKey:@"lastCheckedPrice"];
NSDateComponents *compon = [[NSCalendar currentCalendar] components:(NSUndefinedDateComponent) fromDate:[NSDate date]];
[saveState setObject:[NSString stringWithFormat:@"%ld", (usingYesterdaysPrices ? [[saveState objectForKey:@"day"] intValue] : [compon day])] forKey:@"day"];
[[NSUserDefaults standardUserDefaults] setObject:saveState forKey:@"save"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.priceLabel.text =self.currentPrice;
self.yesterdayLabel.text = [NSString stringWithFormat:@"%@", yesterdaysPrice];
self.changeLabel.text = differenceInPrices;
app.networkActivityIndicatorVisible = NO;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"h:mm:ss aa"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
lstUpdate.text = [NSString stringWithFormat:@"Updated: %@ at %@", theDate, theTime];
refreshing = NO;
}];
我的viewDidLoad中也有这段代码,
[super viewDidLoad];
refreshing = NO;
NSDictionary *save = [[NSUserDefaults standardUserDefaults] objectForKey:@"save"];
if (save) {
int timeStamp = [[save objectForKey:@"day"] intValue];
NSDateComponents *compon = [[NSCalendar currentCalendar] components:(NSUndefinedDateComponent) fromDate:[NSDate date]];
if ([compon day] != timeStamp) {
usingYesterdaysPrices = YES;
yesterdaysPrice = [save objectForKey:@"lastCheckedPrice"];
if (!yesterdaysPrice) {
yesterdaysPrice = @"N/A";
}
}
}
else {
yesterdaysPrice = @"N/A";
}
self.differenceInPrices = @"N/A";
[self sendNewRequest:nil];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(sendNewRequest:) userInfo:Nil repeats:YES];