简体   繁体   English

Swift:表达式类型不明确,没有更多上下文

[英]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.

相关问题 Swift 3中的“表达类型不明确,没有更多上下文” - “Type of expression is ambiguous without more context” in Swift 3 Swift 中没有更多上下文的表达式类型不明确 - Type of expression is ambiguous without more context in Swift Swift 2:表达式在没有更多上下文的情况下是模糊的 - Swift 2: Type of expression is ambiguous without more context 在 swift 中,如果没有更多上下文,表达式类型是模棱两可的? - In swift Type of expression is ambiguous without more context? 斯威夫特:表达类型不明确,没有更多背景? - Swift: Type of Expression is ambiguous without more context? “表达类型是模棱两可的,没有更多上下文”,迅速3 - 'Type of expression is ambiguous without more context' swift 3 在 Swift 中 - 表达式类型不明确,没有更多上下文 - In Swift - Type of expression is ambiguous without more context 没有更多上下文,表达式类型不明确 Swift - Type of expression is ambiguous without more context Swift Swift 2.0类型的表达式是模糊的,没有更多的上下文`.FlexibleHeight` - Swift 2.0 Type of expression is ambiguous without more context `.FlexibleHeight` Swift 2.0:在没有更多上下文的情况下,表达类型不明确-字典 - Swift 2.0: Type of expression is ambiguous without more context - Dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM