当我调用以下函数来读取 dataOfBirth 时,我在设备上测试时不断收到错误访问错误。我在 Xcode 7 beta 中使用 Swift 2.0
func updateUsersAge(){
do{
var error : NSError!
let birthdate = try currentHealthStore.dateOfBirthWithError()
let now = NSDate()
let dateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.NSYearCalendarUnit, fromDate: birthdate, toDate: now, options: NSCalendarOptions.WrapComponents)
let age = dateComponents.year
self.ageValueLabel.text = NSNumberFormatter.localizedStringFromNumber(NSNumber(integer: age), numberStyle: NSNumberFormatterStyle.NoStyle)
}
catch{
print("Not avaialble")
}
}
currentHealthStore 在 AppDelegate 中定义为全局变量:
let currentHealthStore = HKHealthStore()
执行此行后会收到错误:
let birthdate = try currentHealthStore.dateOfBirthWithError()
最佳答案
此代码适用于我的 swift 2、XCode 7.1.1
let birthDay = try healthStore.dateOfBirth()
在错误通过 throws 传回之前,已使用名称 dateOfBirthWithError。
关于ios - HealthKit 中的 EXC_BAD_ACCESS 读取 dateOfBirthWithError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30947703/