ios - HealthKit 中的 EXC_BAD_ACCESS 读取 dateOfBirthWithError

标签 ios swift healthkit

当我调用以下函数来读取 dataOfBirth 时,我在设备上测试时不断收到错误访问错误。我在 Xcode 7 beta 中使用 Swift 2.0

    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 中定义为全局变量:

let currentHealthStore = HKHealthStore()

执行此行后会收到错误:

let birthdate = try currentHealthStore.dateOfBirthWithError()

最佳答案

此代码适用于我的 swift 2、XCode 7.1.1

let birthDay = try healthStore.dateOfBirth()

在错误通过 throws 传回之前,已使用名称 dateOfBirthWithError。

关于ios - HealthKit 中的 EXC_BAD_ACCESS 读取 dateOfBirthWithError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30947703/

相关文章:

ios - 是否可以在没有自动布局的情况下为 iPad 设计 View ?

swift - Firebase iOS 从推送通知接收数据

cocoa-touch - 在类的实例中使用类变量

swift - 点击按钮时更改 UIButton 的图像

ios - 如何在我的 iOS 应用程序中显示 HealthKit 数据?

ios - Swift - 如何保存相关 map 注释(图钉)的图像?

iphone - 自定义键盘为 iPhone sdk 中的应用程序制作?

ios - 如何突出显示文本(在 UILabel 中)并将其转换为 IOS8 swift 中的可点击链接/按钮

swift - 测量特定事件的心率

ios - 使用循环运行后台获取时出现 NSInternalInconsistencyException