2015-06-19 44 views
0

当我调用以下函数来读取dataOfBirth时,我在设备上测试时一直收到错误的访问错误。我在Xcode 7测试EXC_BAD_ACCESS在HealthKit中读取dateOfBirthWithError

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中定义为全局变量使用夫特2.0:

let currentHealthStore = HKHealthStore() 

并且在接收到错误一旦这一行被执行:

let birthdate = try currentHealthStore.dateOfBirthWithError() 
+0

您是否在调试器中使用了dateOfBirthWithError()的代码? – Gruntcakes

+0

是的,没有显示在调试器中,一旦它开始执行此行,我收到此错误 –

+0

发布dateOfBirthWithError的代码? – Tim

回答

0

此代码适用于swift 2,XCode 7.1.1

let birthDay = try healthStore.dateOfBirth() 

在错误传回给出之前已经使用名称dateOfBirthWithError。