首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试从HealthKitStore转换HeartRate时出现异常

尝试从HealthKitStore转换HeartRate时出现异常
EN

Stack Overflow用户
提问于 2015-06-16 20:40:23
回答 1查看 369关注 0票数 1

我正在开发我的第一个iPhone应用程序:一个简单的应用程序,以一种很好的方式显示来自HealthKit的heartRate结果。我的第一步是将结果显示为原始文本。但不幸的是,我在下面的代码行得到了一个异常,告诉我:“线程1信号SIGABRT”。有没有人知道,我做错了什么,并提示我一个方向?

代码语言:javascript
复制
double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]];

代码的其余部分如下所示:

代码语言:javascript
复制
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Set up an HKHealthStore, asking the user for read/write permissions. The profile view controller is the
// first view controller that's shown to the user, so we'll ask for all of the desired HealthKit permissions now.
// In your own app, you should consider requesting permissions the first time a user wants to interact with
// HealthKit data.
if ([HKHealthStore isHealthDataAvailable]) {
    NSSet *writeDataTypes = [self dataTypesToWrite];
    NSSet *readDataTypes = [self dataTypesToRead];

    [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error);

            return;
        }

    }];
}

HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

// Since we are interested in retrieving the user's latest sample
// we sort the samples in descending order by end date
// and set the limit to 1
// We are not filtering the data, and so the predicate is set to nil.
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];

// construct the query & since we are not filtering the data the predicate is set to nil
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:weightType predicate:nil limit:1 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {

    // if there is a data point, dispatch to the main queue
    if (results) {
        dispatch_async(dispatch_get_main_queue(), ^{
            HKQuantitySample *quantitySample = results.firstObject;

            // pull out the quantity from the sample
            HKQuantity *quantity = quantitySample.quantity;

            double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]];
            _HeartRateResults.text = [NSString stringWithFormat:@"%@ lbs", [NSNumberFormatter localizedStringFromNumber:@(usersBeatsPerMinute) numberStyle:NSNumberFormatterNoStyle]];
        });
    }
}];

// do not forget to execute the query after its constructed
[_healthStore executeQuery:query];}
EN

回答 1

Stack Overflow用户

发布于 2015-06-17 04:58:06

documentation中有一个我不太理解的注释(“这些样本使用计数/时间单位”),所以我做了一些搜索并尝试了一下,能够获得一个我使用以下代码手动输入到健康应用程序中的值:

代码语言:javascript
复制
double rate = [mostRecentQuantity doubleValueForUnit:[[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]];

我以前没见过unitDividedByUnit。这是我从the article中提取出来的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30867867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档