HealthKit(展开可选时意外发现 nil)
HealthKit (Unexpectedly found nil when unwrapping an optional)
当我尝试读取 HealthKit 中的数据时,我收到一条错误消息,告诉我应用程序因
而崩溃
fatal error: unexpectedly found nil while unwrapping an Optional value
我知道我正在尝试解包 nil
的可选项,但是当我尝试使用可选项时,我收到一条错误消息,告诉我强制解包它。
这是我正在使用的一些代码:
import Foundation
import HealthKit
import UIKit
class HealthManager {
let healthKitStore = HKHealthStore()
func authorizeHealthKit(completion: ((success: Bool, error: NSError) -> Void)!) {
// Set the Data to be read from the HealthKit Store
let healthKitTypesToRead: Set<HKObjectType> = [(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned))!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierFlightsClimbed)!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierNikeFuel)!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!]
// Check if HealthKit is available
if !HKHealthStore.isHealthDataAvailable() {
let error = NSError(domain: "com.MyCompany.appName", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available on this device"])
if completion != nil {
completion?(success: false, error: error)
}
return;
}
// Request HealthKit Access
self.healthKitStore.requestAuthorizationToShareTypes(nil, readTypes: healthKitTypesToRead) {
(success, error) -> Void in
if completion != nil {
completion?(success: true, error: error!)
}
}
}
}
此外,如果我尝试删除 bang 运算符 (!),我会收到一条错误消息:
Value of optional type 'HKQuantityType?' not unwrapped; did you mean to use '!'?
因为 quantityTypeForIdentifier
returns HKQuantityType?
,然后强制展开它可能会导致展开 nil 值,如您所知。您必须检查是否为 nil,例如以下形式:
if let objectType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierFlightsClimbed) {
// Add objectType to set
}
我意识到当我请求 HealthKit 访问时,这段代码正在返回 nil
:
if completion != nil {
completion?(success: true, error: error!)
}
事实证明 error
实际上是 nil,我强行解开 nil。结果,我将代码更改为:
if error != nil && completion != nil {
completion?(success: true, error: error!)
}
当我尝试读取 HealthKit 中的数据时,我收到一条错误消息,告诉我应用程序因
而崩溃fatal error: unexpectedly found nil while unwrapping an Optional value
我知道我正在尝试解包 nil
的可选项,但是当我尝试使用可选项时,我收到一条错误消息,告诉我强制解包它。
这是我正在使用的一些代码:
import Foundation
import HealthKit
import UIKit
class HealthManager {
let healthKitStore = HKHealthStore()
func authorizeHealthKit(completion: ((success: Bool, error: NSError) -> Void)!) {
// Set the Data to be read from the HealthKit Store
let healthKitTypesToRead: Set<HKObjectType> = [(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned))!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierFlightsClimbed)!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierNikeFuel)!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!, HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!]
// Check if HealthKit is available
if !HKHealthStore.isHealthDataAvailable() {
let error = NSError(domain: "com.MyCompany.appName", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available on this device"])
if completion != nil {
completion?(success: false, error: error)
}
return;
}
// Request HealthKit Access
self.healthKitStore.requestAuthorizationToShareTypes(nil, readTypes: healthKitTypesToRead) {
(success, error) -> Void in
if completion != nil {
completion?(success: true, error: error!)
}
}
}
}
此外,如果我尝试删除 bang 运算符 (!),我会收到一条错误消息:
Value of optional type 'HKQuantityType?' not unwrapped; did you mean to use '!'?
因为 quantityTypeForIdentifier
returns HKQuantityType?
,然后强制展开它可能会导致展开 nil 值,如您所知。您必须检查是否为 nil,例如以下形式:
if let objectType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierFlightsClimbed) {
// Add objectType to set
}
我意识到当我请求 HealthKit 访问时,这段代码正在返回 nil
:
if completion != nil {
completion?(success: true, error: error!)
}
事实证明 error
实际上是 nil,我强行解开 nil。结果,我将代码更改为:
if error != nil && completion != nil {
completion?(success: true, error: error!)
}