0
votes
let healthWrite = Set(arrayLiteral:[
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex),
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned),
    HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning),
    HKQuantityType.workoutType()
])
1
Lookup the documentation. What is the return type of quantityTypeForIdentifier()? What does the question mark in HKQuantityType? indicate? What do you know about optionals and unwrapping? - Martin R

1 Answers

1
votes

The constructor wants an array literal not an array so you'd init with something like this

let healthWrite = Set(arrayLiteral:
     HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
     HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
     HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
     HKQuantityType.workoutType()
     )

More on arrays and array literal https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

EDIT:

It was the optional values that were giving you grief not the "[]"

let healthWrite = Set([
            HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex)!,
            HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
            HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
            HKQuantityType.workoutType()]
            )

Better yet if you use a guard or if let to check that HKObjectType.quantityTypeForIdentifier actually returns a value before creating the set