Iphone 未从服务器接收到信息?

Iphone 未从服务器接收到信息?,iphone,nsurlconnection,Iphone,Nsurlconnection,好的,我的问题很简单。下面是我从API接收价格的代码,在我的代码中,我希望显示今天的当前价格、昨天的价格以及从昨天到今天的百分比变化。不幸的是,只有当前价格显示正确,但昨天的价格和百分比变化只是显示不适用 感谢您的帮助 if (refreshing) return; refreshing = YES; UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES

好的,我的问题很简单。下面是我从API接收价格的代码,在我的代码中,我希望显示今天的当前价格、昨天的价格以及从昨天到今天的百分比变化。不幸的是,只有当前价格显示正确,但昨天的价格和百分比变化只是显示不适用

感谢您的帮助

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];

你需要提供更多的细节。显示返回的JSON。提供更多关于你工作和不工作的细节。任何人都无法在不知道您正在处理哪些数据的情况下知道您的代码的功能。旁注-您在代码中做了一些错误的事情。永远不要通过查看错误变量来检查某些东西是否有效。通常,API会为其他值(或返回值)返回
nil
,然后,也只有在那时才应该查看错误结果。您还可以毫无意义地使用
stringWithFormat:
。只有当你真的需要格式化一个字符串时才使用它。我没有尝试过任何不同的方法。我试图从API调用当前的价格,它运行良好。我试图存储24小时后的价格,并显示为昨天的价格,然后得到这两个值的百分比变化。