2015-06-16 46 views
1

我正在处理我的第一个iPhone应用程序:一个简单的应用程序,以良好的方式显示HealthKit的heartRate结果。我的第一步是将结果显示为原始文本。但不幸的是,我在下面一行发现异常,告诉我:“线程1信号SIGABRT”。有人知道,我做错了什么,并在一个方向上暗示我?尝试从HealthKitStore转换HeartRate时发生异常

double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]]; 

的代码的其余部分是这样的:

- (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];} 
+1

检查数量是否首先兼容。 '[quantity isCompatibleWithUnit:[HKUnit countUnit]];' –

+0

试一试。感谢您的快速响应 – Jonathan

+0

已将代码更改为此,现在它不会崩溃,但它不显示hearRate ether: 'bool compatibilityQuantityAndCountUnit = [quantity isCompatibleWithUnit:[HKUnit countUnit]]; if(compatibilityQuantityAndCountUnit)double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]]; _HeartRateResults.text = [NSString stringWithFormat:@“%@ lbs”,[NSNumberFormatter localizedStringFromNumber:@(usersBeatsPerMinute)numberStyle:NSNumberFormatterNoStyle]];}' – Jonathan

回答

3

有在documentation评论(“这些样品使用次数/时间单位”)我不明白,所以我做了一些搜索,并试图出来,并能得到我手动投入使用此生应用程序的值:

double rate = [mostRecentQuantity doubleValueForUnit:[[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]]; 

我以前没有见过unitDividedByUnit。这里是the article我把它从中拉出来。

+0

这工作。不知道如果没有你的帖子我会怎么想。非常感谢! – Hivebrain

相关问题