EXC_BAD_ACCESS in HealthKit in reading dateOfBirthWithError

197 views Asked by At

When I call the following function to read dataOfBirth, I keep receiving bad access error while testing on device. I'm using Swift 2.0 in Xcode 7 beta

    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 is defined in AppDelegate as global variable:

let currentHealthStore = HKHealthStore()

And the error is received once this line is executed:

let birthdate = try currentHealthStore.dateOfBirthWithError()
1

There are 1 answers

0
Gerd Castan On

this code works for me in swift 2, XCode 7.1.1

let birthDay = try healthStore.dateOfBirth()

The name dateOfBirthWithError has been used before the error was passed back with a throws.