【问题标题】:Expression Type ambiguous without more context Healthkit ios Swift表达式类型不明确,没有更多上下文 Healthkit ios Swift
【发布时间】:2016-06-22 10:24:43
【问题描述】:

我正在关注This 使用 HealthKit 的教程,但我收到了标题中所述的错误

这是我目前的代码。

import Foundation
import UIKit
import HealthKit

class YASHealthKitManager {
let healthKitStore:HKHealthStore = HKHealthStore()    
class func authorizeHealthKit(completion: ((success:Bool, error:NSError!) -> Void)!)
{
    let healthKitTypesToRead  : [String : AnyObject] = Set(arrayLiteral:[

        HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth),
        HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType),
        HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex),
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass),
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight),
        HKObjectType.workoutType()
        ])

    let healthKitTypesToWrite = Set(arrayLiteral:[
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex),
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned),
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning),
        HKQuantityType.workoutType()
        ])

    if !HKHealthStore.isHealthDataAvailable()
    {
        let error = NSError(domain: "com.raywenderlich.tutorials.healthkit", 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)
        }
    }
}

}

这是错误的截图

我不知道代码有什么问题,因为我只是从教程中复制的。

【问题讨论】:

    标签: ios swift swift2 healthkit


    【解决方案1】:

    Set(arrayLiteral: [a, b, c]) 更改为Set(arrayLiteral: a, b, c)

    【讨论】:

    • @UmairAfzal Swift 1 和 2 之间的语法发生了变化。教程已经过时了。
    • 我明白,但是数组应该包含这些 [] 对吧?
    • @UmairAfzal 否。它接受可变参数。见developer.apple.com/library/ios/documentation/Swift/Conceptual/…
    • 我在函数底部收到另一个错误,上面写着“实例成员 'helathkitstore' 不能用于 YASHealthKitManager 类型”你能帮忙吗?
    • @UmairAfzal 尝试将class func 更改为func。您应该将其作为新问题发布。
    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多