【问题标题】:Error 'cannot convert value of type 'int' to expected argument type 'UInt'错误“无法将“int”类型的值转换为预期的参数类型“UInt”
【发布时间】:2016-07-30 01:23:38
【问题描述】:

上线

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

我收到红旗错误

无法将“Int”类型的值转换为预期的参数类型“Uint”

我从另一个 Xcode 项目中复制粘贴了代码,这在另一个项目中没有为该行带来任何错误(仅在 watchOS 上读取心率)。

有什么想法吗?

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))

【问题讨论】:

    标签: swift healthkit


    【解决方案1】:

    该错误准确地告诉您您必须做什么。只需更改代码:

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

    您需要将Int 更改为UInt,这是HKQueryAnchor 所期望的。

    【讨论】:

    • 对于我们这些编码初学者来说,这并不明显。 UInt 是我现在知道的“无符号整数”。还要感谢 Eric D,虽然在您打字的时候已经发现了这一点,但感谢您的确认!
    • @DimitriT 我已经删除了我的评论,因为弗拉基米尔已经发布了正确的答案。提示:在 Xcode 中,养成对变量/方法/对象/等执行 ALT+CLICK 以查看其类型和签名的习惯。在这种情况下,它对学习有很大帮助。 :)
    • 会的 - 谢谢 Eric D,很棒的提示。确实是非常好的学习方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2017-01-09
    • 2017-02-08
    相关资源
    最近更新 更多