-->

Error 'cannot convert value of type 'int&#

2019-09-10 08:46发布

问题:

On the line

var anchor = HKQueryAnchor(fromValue: Int(HKAnchoredObjectQueryNoAnchor))

I get the red flag error

cannot convert value of type 'Int' to expected argument type 'Uint'

I had copy-pasted the code from another Xcode project, which brought no error for this line in the other project (reading heart rate on watchOS only).

Any ideas?

let healthStore = HKHealthStore()

//State of the app - is the workout activated
var workoutActive = false

// define the activity type and location
var workoutSession : HKWorkoutSession?
let heartRateUnit = HKUnit(fromString: "count/min")
var anchor = HKQueryAnchor(fromValue: Int(HKAnchoredObjectQueryNoAnchor))

回答1:

The error is telling you exactly what you have to do. Just change the code:

var anchor = HKQueryAnchor(fromValue: UInt(HKAnchoredObjectQueryNoAnchor))

You need to change Int for UInt, which is what HKQueryAnchor expects.