Hardwell
Hardwell

Reputation: 1

Swift: Type of expression is ambiguous without more context

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

Upvotes: 0

Views: 1183

Answers (1)

Moriya
Moriya

Reputation: 7906

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

Upvotes: 1

Related Questions