ios - 授权给 HealthKit 时,错误 '(_, _) -> Void' 无法转换为 'HealthManager'

标签 ios swift swift2 healthkit

这里是HealthManager类,里面有一个函数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)")
        }
      }
    }
  }

最佳答案

您正在调用上的实例方法。要调用实例方法,您必须先创建一个实例,例如

let manager = HealthManager()
manager.authorizeHealthKit { ... }

您还可以将该方法设为方法:

class func authorizeHealthKit(...)

关于ios - 授权给 HealthKit 时,错误 '(_, _) -> Void' 无法转换为 'HealthManager',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37168824/

相关文章:

json - 将 FIRTimestamp 转换为 JSON

ios - 如何删除我的应用程序 ios swift 中存储的数据?

ios - 如何使用这个格式化程序?

ios - ScrollView 区域

ios - 我如何优雅地从一个嵌入式 View 导航到另一个(在 IB 中使用布局)?

swift - 如何构建自定义 Swift 框架以及它与 SPM 有何关系?

ios - Facebook SDK 4.0 IOS Swift 以编程方式注销用户

ios - 在 Swift 中声明实例变量 : optional vs forced unwrapping vs initial value

ios - Swift 中的服务定位器模式

iOS-swift 如何制作带分页的表格 View ?