[英]Swift: Type of expression is ambiguous without more context
let healthWrite = Set(arrayLiteral:[
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMassIndex),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned),
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning),
HKQuantityType.workoutType()
])
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 有关数组和数组文字的更多信息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 更好的是,如果使用
guard
或者if let
在创建集合之前检查HKObjectType.quantityTypeForIdentifier
实际返回值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.