gpt4 book ai didi

ios - HealthKit 中的 EXC_BAD_ACCESS 读取 dateOfBirthWithError

转载 作者:行者123 更新时间:2023-11-30 14:17:30 25 4
gpt4 key购买 nike

当我调用以下函数来读取 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com