2015-06-19 41 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。