从swift 1.2迁移后,swift2中的healthKit错误

时间:2015-08-05 08:03:07

标签: ios swift swift2 health-kit

我有这个代码要求在Swift 1.2中工作的写权限,升级到Swift 2.0后我收到一个奇怪的错误: ... '_' is not convertible to 'HKWorkoutType'

出现错误的代码行:

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

任何想法?

1 个答案:

答案 0 :(得分:5)

为前两项添加!

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

这是必需的,因为quantityTypeForIdentifier会返回HKQuantityType?