错误 '(_, _) -> Void' 在授权给 HealthKit 时不能转换为 'HealthManager'
error '(_, _) -> Void' is not convertible to 'HealthManager' when authorize to HealthKit
这里是 HealthManager Class 和其中的一个函数 authorizeHealthKit。所有这些都在 HealthManager.swift 文件中。
class HealthManager {
func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!)
{
// 1. Set the types you want to read from HK Store
let healthKitTypesToRead : Set = [
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.workoutType()
]
// 2. Set the types you want to write to HK Store
let healthKitTypesToWrite : Set = [
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
HKQuantityType.workoutType()
]
// 3. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable()
{
let error = NSError(domain: "Ira.HKTutorial", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
if( completion != nil )
{
completion(success:false, error:error)
}
return;
}
// 4. Request HealthKit authorization
healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
if( completion != nil )
{
completion(success:success,error:error)
}
}
}
}
问题是当我试图在 ViewController.swift:
中调用 authorizeHealthKit() 方法时
func authorizeHealthKit()
{
HealthManager.authorizeHealthKit {(authorized, error) -> Void in//here is an error '(_, _) -> Void' is not convertible to 'HealthManager'
if authorized {
println("HealthKit authorization received.")
}
else
{
println("HealthKit authorization denied!")
if error != nil {
println("\(error)")
}
}
}
}
您正在 class 上调用 实例 方法。要调用实例方法,您必须先创建一个实例,例如
let manager = HealthManager()
manager.authorizeHealthKit { ... }
您也可以将方法设为 class 方法:
class func authorizeHealthKit(...)
这里是 HealthManager Class 和其中的一个函数 authorizeHealthKit。所有这些都在 HealthManager.swift 文件中。
class HealthManager {
func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!)
{
// 1. Set the types you want to read from HK Store
let healthKitTypesToRead : Set = [
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.workoutType()
]
// 2. Set the types you want to write to HK Store
let healthKitTypesToWrite : Set = [
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
HKQuantityType.workoutType()
]
// 3. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable()
{
let error = NSError(domain: "Ira.HKTutorial", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
if( completion != nil )
{
completion(success:false, error:error)
}
return;
}
// 4. Request HealthKit authorization
healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
if( completion != nil )
{
completion(success:success,error:error)
}
}
}
}
问题是当我试图在 ViewController.swift:
中调用 authorizeHealthKit() 方法时 func authorizeHealthKit()
{
HealthManager.authorizeHealthKit {(authorized, error) -> Void in//here is an error '(_, _) -> Void' is not convertible to 'HealthManager'
if authorized {
println("HealthKit authorization received.")
}
else
{
println("HealthKit authorization denied!")
if error != nil {
println("\(error)")
}
}
}
}
您正在 class 上调用 实例 方法。要调用实例方法,您必须先创建一个实例,例如
let manager = HealthManager()
manager.authorizeHealthKit { ... }
您也可以将方法设为 class 方法:
class func authorizeHealthKit(...)