当我调用以下函数来读取dataOfBirth时,我在设备上测试时一直收到错误的访问错误。我在Xcode 7 beta中使用Swift 2.0
special-symbol?
currentHealthStore在AppDelegate中定义为全局变量:
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")
}
}
执行此行后会收到错误:
let currentHealthStore = HKHealthStore()
答案 0 :(得分:0)
此代码适用于swift 2,XCode 7.1.1
let birthDay = try healthStore.dateOfBirth()
在错误传回错误之前,已使用名称dateOfBirthWithError。