【问题标题】:'Argument passed to call that takes no arguments' instantiating HKWorkoutRouteQuery“参数传递给不带参数的调用”实例化 HKWorkoutRouteQuery
【发布时间】:2018-06-24 22:12:52
【问题描述】:

我正在尝试读取 HKWorkout 的路线数据,但我无法读取,因为当我尝试实例化 HKWorkoutRouteQuery我检索到的样本。

根据Apple's documentation,我的代码如下所示:

func getRouteData() -> [(Double)] {
    // Return early if not a distance workout
    guard self.shouldShowDistance else {
        return []
    }

    let store = HKHealthStore()
    let runningObjectQuery = HKQuery.predicateForObjects(from: self.workout)
    let routeQuery = HKAnchoredObjectQuery(type: HKSeriesType.workoutRoute(), predicate: runningObjectQuery, anchor: nil, limit: HKObjectQueryNoLimit) { (query, samples, deletedObjects, anchor, error) in

        guard error == nil else {
            // Handle any errors here.
            fatalError("query failed")
        }

        guard samples != nil else {
            fatalError("No samples")
        }

        guard samples!.count > 0 else { fatalError("No samples") }

        guard let route = samples?.first as? HKWorkoutRoute else {
            fatalError("No samples")
        }

        // Create the route query.
        let query = HKWorkoutRouteQuery(route: route) { (query, locationsOrNill, done, errorOrNil) in

            // This block may be called multiple times.

            if let error = errorOrNil {
                // Handle any errors here.
                return
            }

            guard let locations = locationsOrNil else {
                fatalError("*** Invalid State: This can only fail if there was an error. ***")
            }

            // Do something with this batch of location data.

            if done {
                // The query returned all the location data associated with the route.
                // Do something with the complete data set.
            }

            // You can stop the query by calling:
            // store.stop(query)

        }
        store.execute(query)

    }

    store.execute(routeQuery)

    return []
}

这对我来说毫无意义,因为 Apple 自己的文档要求 HKWorkoutRouteQuery 使用示例进行实例化。

任何帮助将不胜感激。非常感谢。

【问题讨论】:

    标签: ios swift ios11 healthkit xcode9.4


    【解决方案1】:

    Apple 通过 Bug Reporter 回复了我。问题是我忽略了导入 CoreLocation。您可以通过将以下行添加到文件顶部来修复此错误。

    import CoreLocation
    

    顺便说一句,我认为这是一个很好的例子,说明了编写描述性错误的重要性。如果错误提到了需要 CoreLocation 的结果完成,我可以自己解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多