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()
您是否在調試器中使用了dateOfBirthWithError()的代碼? – Gruntcakes
是的,沒有顯示在調試器中,一旦它開始執行此行,我收到此錯誤 –
發佈dateOfBirthWithError的代碼? – Tim